Introduction to Python Counter
Python provides a counter class that is the subclass of the collections modules. It is a special class of object data set. We also have container data types that are part of the Collections module. The counter subclass is generally used for counting the hashable objects. When the counter is executed, it creates the hash table of iterable items. The counter object always returns an itertool for every element in the counter object.
The counter is a dictionary subclass used for counting the objects that are hashable. Elements inside the counter are stored as dictionary keys, and counts are stored as dictionary values. Counts can be any integer value. It also includes zero and negatives values.
counter = Counter() # this will return into empty counter
Examples of Python Counter
Following are the examples of Python Counter
from collections import Counter c = Counter() print(c) c = Counter('sample string') print(c) c = Counter(['word', 'word1','word1']) print(c) c = Counter(a=4, b=2, c=0, d=-2) sorted(c.elements())
Code Explanation: In the above example, we have imported the counter subclass from the collections module. Then we have created the empty counter. Then we passed the string of two words into the counter function “sample string” and printed the counter the counter function’s output. You can see that the counter function has given the count of each and every character in the string and the number of times that character is repeating. Then we have passed the list of the words into the counter function. The list contains three words; now, if we print the counter function result this time, we will get each word’s count. Then we have the character and assigned some number to it inside the counter function. Now we are trying to print the elements of the counter function using the sorted function. The sorted function will sort the list elements. Now you can see that the counter function has printed the character as many times as the number we have assigned to it. The counter function will not execute any integer equal to zero or less than zero.
from collections import Counter lst = ['a', 'b', 'c', 'b', 'a', 'b'] c = Counter(lst) print(c)
Code Explanation: In the above example, we have created a list of the repeating characters. Now we are passing the list into the counter function and printing the result. As a result, the counter function outputs the count of each character from the list.
from collections import Counter counter = Counter(a=3, b=2, c=4) print(counter) print(list(counter.elements()))
Code Explanation: In the above example, we have some character inside the counter function and assigned some number to it, and then we are printing the result of the counter function using elements, and we can see its printing the character equal to the number of the value that we have assigned.
from collections import Counter c = Counter('sssssshhhhhcccdddd') c.most_common(3)
Code Explanation: In the above example, we have passed the string inside the counter function, and we printed the result using the most_common method. This method takes an optional parameter of integer and will output the result having count to the most common or only output number of elements. As you can see, we have passed 3 inside the most_common method, so it will result in only three elements having maximum counts. If we don’t pass any value, it will result in all the elements.
from collections import Counter a = Counter(a=5, b=4, c=2, d=-3) b = Counter(a=3, b=1, c=4, d=1) a.subtract(b) a
Code Explanation: In the above example, we have created two counters, a and b. Both counters consist of elements and assigned some numbers to it. Now we are using the subtract method for subtracting the count of b from a. Now the counter function will result in the final count of elements after subtracting each element’s count. Here, in this case, both of the inputs can be zero and negative also. The number of elements on the counter can be different.
from collections import Counter a = Counter(a=5, b=4, c=2, d=-3) b = Counter(a=3, b=1, c=4, d=1) a+b
If we do this, it will also add both counters’ elements and result into their sum.
from collections import Counter a = Counter(a=5, b=4, c=2, d=-3) b = Counter(a=3, b=1, c=4, d=1) a-b
If we do this, it will subtract the count of b from a counter and in the result, it will show only positive counts; negative or zero counts are not shown.
The counter is a dict type of subclass for counting the hashable objects. Whenever we execute a counter method, it creates the dictionary keys, and the counts are stored as dictionary value. The counter method only outputs positive counts; it will ignore zero and negative values. We can also perform several mathematical operations like addition, subtraction, AND, OR, Union.
This is a guide to Python Counter. Here we also discuss the Introduction and syntax of the python counter along with different examples and code implementation. You may also have a look at the following articles to learn more –