cllections python code example
Example 1: python counter
>>>
>>> cnt = Counter()
>>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
... cnt[word] += 1
>>> cnt
Counter({'blue': 3, 'red': 2, 'green': 1})
>>>
>>> import re
>>> words = re.findall(r'\w+', open('hamlet.txt').read().lower())
>>> Counter(words).most_common(10)
[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
('you', 554), ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]
Example 2: python counter
sum(c.values())
c.clear()
list(c)
set(c)
dict(c)
c.items()
Counter(dict(list_of_pairs))
c.most_common()[:-n-1:-1]
c += Counter()