[PYTHON] Count up list using collections.Counter

a = ["poko", "poko", "hoge", "aaa", "aaa", "poko", "aaa", "aab"]

There is a list like

from collections import Counter
counts = Counter(a)
#Top 2
print counts.most_common(2)
# [Out]: [('poko', 3), ('aaa', 3)]
 
#All from the top
print counts.most_common()
# [Out]: [('poko', 3), ('aaa', 3), ('aab', 1), ('hoge', 1)]

#key specification
print counts[“poko”]
# [Out]: 3

Can be used like a sorted dict (is it okay to say something like this?)

Recommended Posts

Count up list using collections.Counter
[Python] Not just count collections.Counter
Search / list synonyms using Japanese WordNet