counter module in python code example

Example 1: counter method in python

from collections import Counter
list1 = ['x','y','z','x','x','x','y','z']
print(Counter(list1))

Example 2: collections.counter in python

>>> from collections import Counter
>>> 
>>> myList = [1,1,2,3,4,5,3,2,3,4,2,1,2,3]
>>> print Counter(myList)
Counter({2: 4, 3: 4, 1: 3, 4: 2, 5: 1})
>>>
>>> print Counter(myList).items()
[(1, 3), (2, 4), (3, 4), (4, 2), (5, 1)]
>>> 
>>> print Counter(myList).keys()
[1, 2, 3, 4, 5]
>>> 
>>> print Counter(myList).values()
[3, 4, 4, 2, 1]

Example 3: import counter python

from collections import Counter

Example 4: counter method in python

Counter({'o': 3, ' ': 3, 'u': 3, 'e': 2, 'l': 2, 't': 2, 'r': 2, '9': 2, 'W': 1,
 'c': 1, 'm': 1, 'G': 1, 'T': 1, 'i': 1, 'a': 1, 's': 1, '!': 1})

Example 5: counter method in python

Counter({'x': 4, 'y': 2, 'z': 2})

Example 6: counter method in python

from collections import Counter
my_str = "Welcome to Guru99 Tutorials!"
print(Counter(my_str))

Tags:

Misc Example