collections.Counter() - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code sharing (https://python-forum.io/forum-5.html) +--- Thread: collections.Counter() (/thread-41863.html) |
collections.Counter() - pynerds - Mar-29-2024 The Counter() in the collections module is a dictionary-like object that keeps distinct elements as keys with their counts as the values. We can use the class to get the occurrences of each element in a given iterable. #import the Counter class from collections module from collections import Counter #An iterable with elements to count data = 'aabbbccccdeefff' #create the Counter object c = Counter(data) print(c) #get the count of a specific element print(c['f'])
|