Counter()

Counter is a subclass of dictionary with keys as elements and their counts as values.
from collections import Counter
my_list=['One','Two','One','Three']
my_counter=Counter(my_list) 
print(my_counter)    # Counter({'One': 2, 'Two': 1, 'Three': 1})
We can read any values
print(my_counter['Two']) # 1
Using a string
from collections import Counter
my_str='Hello'
my_counter=Counter(my_str)
print(my_counter)  # Counter({'l': 2, 'H': 1, 'e': 1, 'o': 1})
Using a list
from collections import Counter
my_list=[1,3,4,2,3,2]
my_counter=Counter(my_list)
print(my_counter)         # Counter({3: 2, 2: 2, 1: 1, 4: 1})
Getting type() of counter
from collections import Counter
my_list=[1,3,4,2,3,2]
my_counter=Counter(my_list)
print(type(my_counter)) # <class 'collections.Counter'>

Using items(), keys(), values()

We can use all these methods, here are sample scripts.
from collections import Counter
my_list=[1,3,4,2,3,2]
my_counter=Counter(my_list)
for k,v in my_counter.items():
  print("Keys:",k,", Values:", v)
Output
Keys: 1 , Values: 1
Keys: 3 , Values: 2
Keys: 4 , Values: 1
Keys: 2 , Values: 2
Using keys()
from collections import Counter
my_list=[1,3,4,2,3,2]
my_counter=Counter(my_list)
for k in my_counter.keys():
  print("Keys:",k)
Output
Keys: 1
Keys: 3
Keys: 4
Keys: 2
Using values()
from collections import Counter
my_list=[1,3,4,2,3,2]
my_counter=Counter(my_list)
for v in my_counter.values():
  print("Values :",v)
Output
Values : 1
Values : 2
Values : 1
Values : 2
Getting the Key and value of element with Maximum value.
from collections import Counter
my_list=[1,3,4,2,3,2,3,1,3]
my_counter=Counter(my_list)
key_of_max_value = max(my_counter, key=my_counter.get) 
print(key_of_max_value,my_counter[key_of_max_value])
Output
3 4 

elements()

By using elements() method we can get the original list.
from collections import Counter
my_list=[1,3,4,2,3,2]
my_counter=Counter(my_list)
print(my_counter)                  # Counter({3: 2, 2: 2, 1: 1, 4: 1})
print(list(my_counter.elements())) # [1, 3, 3, 4, 2, 2]

most_common()

most_common(n) returns frequently used elements as keys and counts as values. The optional parameter n can be used to restrict the number of elements to return.
from collections import Counter
my_list=[1,3,4,2,3,2]
my_counter=Counter(my_list)
print(my_counter.most_common())  #  [(3, 2), (2, 2), (1, 1), (4, 1)] 
from collections import Counter
my_list=[1,3,4,2,3,2]
my_counter=Counter(my_list)
my_common=my_counter.most_common()
for no,count in my_common:
  #print("elements =   %d: %d" % (no, count))
  print("Element = {}: Occurrences {}".format(no, count))
Getting 2 elements only.
from collections import Counter
my_list=[1,3,4,2,3,2]
my_counter=Counter(my_list)
my_common=my_counter.most_common(2)
for no,count in my_common:
    print("Element = {}: Occurrences {}".format(no, count))
Output
Element = 3: Occurrences 2
Element = 2: Occurrences 2

update()

Counter is not immutable , so we can add elements
from collections import Counter
my_list=[1,3,4,2,3,2]
my_counter=Counter(my_list)
my_counter.update([8,9])  # add two more elements 
print(my_counter)  # Counter({3: 2, 2: 2, 1: 1, 4: 1, 8: 1, 9: 1})
We can add existing elements
from collections import Counter
my_list=[1,3,4,2,3,2]
my_counter=Counter(my_list)
my_counter.update([3,1])  # add two more elements 
print(my_counter)  # Counter({3: 3, 1: 2, 2: 2, 4: 1})
We can initialize counter and then add elements.
from collections import Counter
my_counter=Counter()
my_list=[1,3,3,2]
my_counter.update(my_list)
print(my_counter)  # Counter({3: 2, 1: 1, 2: 1})

clear()

Clear the Counter.
from collections import Counter
my_list=[1,3,3,2]
my_counter=Counter(my_list)
print(my_counter)   #   Counter({3: 2, 1: 1, 2: 1})
print(my_counter.clear()) # None

Counter Arithmetic

Add, subtract, and, or
from collections import Counter
my_list1=Counter([1,3,3,2])
my_list2=Counter([3,4,5,2])
print(my_list1 + my_list2) # Counter({3: 3, 2: 2, 1: 1, 4: 1, 5: 1})
print(my_list1 - my_list2) # Counter({1: 1, 3: 1})
print(my_list1 & my_list2) # Counter({3: 1, 2: 1})
print(my_list1 | my_list2) # Counter({3: 2, 1: 1, 2: 1, 4: 1, 5: 1})
Using string and split() we created list and then counter.
from collections import Counter
my_str1='Welcome to plus2net'
my_str2='Welcome to Python'
my_counter1=Counter(my_str1.split(' '))
my_counter2=Counter(my_str2.split(' '))
print(my_counter1 - my_counter2)
print(my_counter1 + my_counter2)
print(my_counter1 & my_counter2)
print(my_counter1 | my_counter2)
Output
Counter({'plus2net': 1})
Counter({'Welcome': 2, 'to': 2, 'plus2net': 1, 'Python': 1})
Counter({'Welcome': 1, 'to': 1})
Counter({'Welcome': 1, 'to': 1, 'plus2net': 1, 'Python': 1})
We will get the common words used in two strings by using & operation.
print(my_counter1 & my_counter2)



Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer