Python - Count Hashable Objects
Last Updated: 1720Z 23SEP19
(Created: 1720Z 23SEP19)
Count occurances of hashable objects (eg: words)
Signatures:
class collections.Counter([iterable or mapping])
Examples:
from collections import Counter
words = "if there was there was but if there was not there was not".split()
counts = Counter(words)
print(counts)
# Counter({'was': 4, 'there': 4, 'not': 2, 'if': 2, 'but': 1})
print(counts.most_common(2))
# [('was', 4), ('there', 4)]
References: