5 ms·
For Python 2.7 there's collections.Counter [1]: import collections c = collections.Counter('supercalifragilisticexpialidocius') print c # Count
by ciupicri 14y ago
For Python 2.7 there's collections.Counter [1]:
import collections
c = collections.Counter('supercalifragilisticexpialidocius')
print c
# Counter({'i': 7, 'a': 3, 'c': 3, 'l': 3, 's': 3, 'e': 2,
# 'p': 2, 'r': 2, 'u': 2, 'd': 1, 'g': 1, 'f': 1,
# 'o': 1, 't': 1, 'x': 1})
[1] http://docs.python.org/2.7/library/collections.html#collections.Counter http://docs.python.org/2.7/library/collections.html#collecti...
- andreasvc 14y agoIt's odd that with all the 'high-brow' datatype names such as dictionaries, tuples, and sets, they decided to call this one Counter instead of the obvious choice 'multiset' (or 'bag'); it supports set operations such as intersection, so it's more generally useful than just collecting top ten lists &c.
- deleted 14y ago[deleted]