Example 1: counter method in python
from collections import Counter
list1 = ['x','y','z','x','x','x','y','z']
print(Counter(list1))
Example 2: import counter python
from collections import Counter
Example 3: 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 4: counter method in python
Counter({'x': 4, 'y': 2, 'z': 2})
Example 5: counter method in python
from collections import Counter
my_str = "Welcome to Guru99 Tutorials!"
print(Counter(my_str))
Example 6: python counter
>>> c = Counter(a=4, b=2, c=0, d=-2)
>>> d = Counter(a=1, b=2, c=3, d=4)
>>> c.subtract(d)
>>> c
Counter({'a': 3, 'b': 0, 'c': -3, 'd': -6})