Example 1: import counter python
from collections import Counter
Example 2: python counter
>>>
>>> cnt = Counter()
>>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
... cnt[word] += 1
>>> cnt
Counter({'blue': 3, 'red': 2, 'green': 1})
>>>
>>> import re
>>> words = re.findall(r'\w+', open('hamlet.txt').read().lower())
>>> Counter(words).most_common(10)
[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
('you', 554), ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]
Example 3: python counter
>>> Counter('abracadabra').most_common(3)
[('a', 5), ('r', 2), ('b', 2)]
Example 4: counter in python
from collections import Counter
x = Counter (a = 2, x = 3, b = 3, z = 1, y = 5, c = 0, d = -3)
for i in x.elements():
print( "% s : % s" % (i, x[i]), end ="\n")