1>>> # Tally occurrences of words in a list
2>>> cnt = Counter()
3>>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
4... cnt[word] += 1
5>>> cnt
6Counter({'blue': 3, 'red': 2, 'green': 1})
7
8>>> # Find the ten most common words in Hamlet
9>>> import re
10>>> words = re.findall(r'\w+', open('hamlet.txt').read().lower())
11>>> Counter(words).most_common(10)
12[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
13 ('you', 554), ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]
14
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
1points = [1, 4, 2, 9, 7, 8, 9, 3, 1]
2x = points.count(9)
3#prints how many times 9 occured in the list