Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created July 10, 2020 03:26
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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})
elem=c.elements()
#returns an iterator
print (elem)#Output:<itertools.chain object at 0x0240EC10>
#converting iterator to list object
print (list(elem))#Output:['a', 'a', 'b', 'c', 'c', 'c', 'c', 'd', 'd', 'd']
c1=Counter({'a':0,'b':2})
#Elements with count less than one are ignored.
print (list(c1.elements()))#Output:['b', 'b']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment