Operations on Python Counter - GeeksforGeeks

18 Dec, 2021
The counter can be used to calculate the frequency in a list or in a string because as any list or string is passed as input, it returns output as a dictionary having keys are the unique elements of the list and values are the corresponding frequencies of the elements. In the code given below, Counter from Collection Python library is imported to find the frequency of every unique character occurring in this string, By using Counter on the list, the frequency of each unique elements present in the list can also be found.
Python3
# Write Python3 code here# Counter used in string to find frequencies of all its unique Characters from collections import Counters1 ='aabbbcccdeff'c1 = Counter(s1)print("c1 :", c1) # Counter used in List to find frequencies of all its unique elements of listL1 =[1, 2, 1, 1, 4, 4, 4, 5, 6, 6, 3, 3, 0, 0]t1 = Counter(L1) print("t1 :", t1)
c1 : Counter({'b': 3, 'c': 3, 'a': 2, 'f': 2, 'e': 1, 'd': 1})
t1 : Counter({1: 3, 4: 3, 0: 2, 3: 2, 6: 2, 2: 1, 5: 1})
 
Performing certain operations on returned output dictionary if d is output dictionary then like d.items(), d.keys(), d.values()
Python3
from collections import Counterd ='aabbbcccdeff'd = Counter(d) # To print Counter valueprint("d :", d) # To access the values corresponds to each keys of the returned dictionaryprint("d.values() : ", d.values())  # To get the keys and values both of dictionaryprint("d.items() :", d.items()) # To get only the keysprint("d.keys() :", d.keys()) # To sort the values of dictionaryprint("sorted(d) :", sorted(d))
d : Counter({'b': 3, 'c': 3, 'a': 2, 'f': 2, 'e': 1, 'd': 1})
d.values() :  dict_values([2, 3, 3, 2, 1, 1])
d.items() : dict_items([('a', 2), ('b', 3), ('c', 3), ('f', 2), ('e', 1), ('d', 1)])
d.keys() : dict_keys(['a', 'b', 'c', 'f', 'e', 'd'])
sorted(d) : ['a', 'b', 'c', 'd', 'e', 'f']
 
Addition of two Counters Addition of two counter creates the additions of values corresponds to each keys, and if a key that is present in one counter and not in other, in that case this key value also get into final output. 
Python3
from collections import Countert1 = Counter('aabbddffggjik')t2 = Counter('aaabbbssshhhggdkkll') print("t1:", t1)print("t2:", t2)print("t1 + t2 :", t1 + t2)
t1: Counter({'g': 2, 'a': 2, 'b': 2, 'f': 2, 'd': 2, 'k': 1, 'j': 1, 'i': 1})
t2: Counter({'a': 3, 'b': 3, 'h': 3, 's': 3, 'l': 2, 'g': 2, 'k': 2, 'd': 1})
t1+t2 : Counter({'a': 5, 'b': 5, 'g': 4, 'k': 3, 'h': 3, 'd': 3, 's': 3, 'l': 2, 'f': 2, 'j': 1, 'i': 1})
 
Subtraction of two Counters Counts of common elements are subtracted from each other and (keeps only positive counts) 
Python3
from collections import Countert1 = Counter('aabbddffggjik')t2 = Counter('aaabbbssshhhggdkkll') print("t1:", t1)print("t2:", t2)print("t1-t2 :", t1-t2)print("t2-t1 :", t2-t1)
t1: Counter({'f': 2, 'd': 2, 'b': 2, 'a': 2, 'g': 2, 'k': 1, 'i': 1, 'j': 1})
t2: Counter({'h': 3, 'b': 3, 'a': 3, 's': 3, 'l': 2, 'k': 2, 'g': 2, 'd': 1})
t1-t2 : Counter({'f': 2, 'i': 1, 'j': 1, 'd': 1})
t2-t1 : Counter({'h': 3, 's': 3, 'l': 2, 'k': 1, 'b': 1, 'a': 1})
 
Intersections(&) of two Counters Intersection(&) will keep only the minimum of corresponding counts: min(t1[x], t2[x]): 
Python3
from collections import Countert1 = Counter('aaabbbbccdeeee')t2 = Counter('aabbccccdddee') print("t1 :", t1)print("t2 :", t2)print("t1&t2 :", t1&t2)
t1 : Counter({'e': 4, 'b': 4, 'a': 3, 'c': 2, 'd': 1})
t2 : Counter({'c': 4, 'd': 3, 'a': 2, 'e': 2, 'b': 2})
t1&t2 : Counter({'c': 2, 'a': 2, 'e': 2, 'b': 2, 'd': 1})
 
Union(|) of two Counters Union(|) will keep only the maximum of corresponding counts: max(c[x], d[x]): 
Python3
from collections import Countert1 = Counter('aaabbbbccdeeee')t2 = Counter('aabbccccdddee') print("t1 :", t1)print("t2 :", t2)print("t1|t2 :", t1|t2)
t1 : Counter({'b': 4, 'e': 4, 'a': 3, 'c': 2, 'd': 1})
t2 : Counter({'c': 4, 'd': 3, 'a': 2, 'b': 2, 'e': 2})
t1|t2 : Counter({'b': 4, 'e': 4, 'c': 4, 'a': 3, 'd': 3})
 
adnanirshad158
sweetyty
varshagumber28
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Split string into list of characters
Python | Convert a list to dictionary
How to print without newline in Python?