Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created Jul 10, 2020
Embed
What would you like to do?
from collections import Counter
c=Counter({'a':2,'b':1,'c':4,'d':3})
print (c)#Output:Counter({'c': 4, 'd': 3, 'a': 2, 'b': 1})
#n is not mentioned. It returns all elements in the counter from most common to least
most=c.most_common()
print (most)#Output:[('c', 4), ('d', 3), ('a', 2), ('b', 1)]
#n is mentioned as 2. It returns only 2 elements having highest count
most1=c.most_common(2)
print (most1)#Output:[('c', 4), ('d', 3)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment