1>>> from collections import Counter
2>>>
3>>> myList = [1,1,2,3,4,5,3,2,3,4,2,1,2,3]
4>>> print Counter(myList)
5Counter({2: 4, 3: 4, 1: 3, 4: 2, 5: 1})
6>>>
7>>> print Counter(myList).items()
8[(1, 3), (2, 4), (3, 4), (4, 2), (5, 1)]
9>>>
10>>> print Counter(myList).keys()
11[1, 2, 3, 4, 5]
12>>>
13>>> print Counter(myList).values()
14[3, 4, 4, 2, 1]
15
1from collections import Counter
2list1 = ['x','y','z','x','x','x','y','z']
3print(Counter(list1))
4
1$ python collections_counter_init.py
2
3Counter({'b': 3, 'a': 2, 'c': 1})
4Counter({'b': 3, 'a': 2, 'c': 1})
5Counter({'b': 3, 'a': 2, 'c': 1})
6
1# import Counter from collections
2from collections import Counter
3
4# creating a raw data-set using keyword arguments
5x = Counter (a = 2, x = 3, b = 3, z = 1, y = 5, c = 0, d = -3)
6
7# printing out the elements
8for i in x.elements():
9 print( "% s : % s" % (i, x[i]), end ="\n")
10
1import collections
2
3print collections.Counter(['a', 'b', 'c', 'a', 'b', 'b'])
4print collections.Counter({'a':2, 'b':3, 'c':1})
5print collections.Counter(a=2, b=3, c=1)
6
1from collections import Counter
2strl = "aabbaba"
3print(Counter(str1))
4
5Counter({'a': 4, 'b': 3})
6