To use Python's Counter frequency statistics function, collections
the module should be introduced. The specific method is:
from collections import Counter
By introducing collections
a module, you can use its Counter
classes to perform frequency statistics. For example:
lst = ['apple', 'banana', 'orange', 'apple', 'banana', 'grape', 'grape', 'grape']
cnt = Counter(lst)
print(cnt)
Output:
Counter({'grape': 3, 'apple': 2, 'banana': 2, 'orange': 1})
Among them, Counter()
the function will accept an iterable object (such as a list, string, tuple, etc.) as input and return a dictionary containing the number of occurrences of each element.