Python Tutorial

Tuesday, February 19, 2013

Python collections - Counter

Counter is used for counting hashable object.

All source code available on github

from collections import Counter

a=[1,2,3,4,2,3,4,45,4,7,8]
counter = Counter(a)

print counter
print counter.values()
print counter.keys()


Output:
Counter({4: 3, 2: 2, 3: 2, 1: 1, 7: 1, 8: 1, 45: 1})
[1, 2, 2, 3, 1, 1, 1]
[1, 2, 3, 4, 7, 8, 45]

0 comments:

Post a Comment