collections.Counter¶
-
class
collections.Counter(*args, **kwds)[source]¶ Dict subclass for counting hashable items. Sometimes called a bag or multiset. Elements are stored as dictionary keys and their counts are stored as dictionary values.
>>> c = Counter('abcdeabcdabcaba') # count elements from a string
>>> c.most_common(3) # three most common elements [('a', 5), ('b', 4), ('c', 3)] >>> sorted(c) # list all unique elements ['a', 'b', 'c', 'd', 'e'] >>> ''.join(sorted(c.elements())) # list elements with repetitions 'aaaaabbbbcccdde' >>> sum(c.values()) # total of all counts 15
>>> c['a'] # count of letter 'a' 5 >>> for elem in 'shazam': # update counts from an iterable ... c[elem] += 1 # by adding 1 to each element's count >>> c['a'] # now there are seven 'a' 7 >>> del c['b'] # remove all 'b' >>> c['b'] # now there are zero 'b' 0
>>> d = Counter('simsalabim') # make another counter >>> c.update(d) # add in the second counter >>> c['a'] # now there are nine 'a' 9
>>> c.clear() # empty the counter >>> c Counter()
Note: If a count is set to zero or reduced to zero, it will remain in the counter until the entry is deleted or the counter is cleared:
>>> c = Counter('aaabbc') >>> c['b'] -= 2 # reduce the count of 'b' by two >>> c.most_common() # 'b' is still in, but its count is zero [('a', 3), ('c', 1), ('b', 0)]
Methods¶
__add__(other) |
Add counts from two counters. |
__and__(other) |
Intersection is the minimum of corresponding counts. |
__contains__((k) -> True if D has a key k, ...) |
|
__delitem__(elem) |
Like dict.__delitem__() but does not raise KeyError for missing values. |
__format__ |
default object formatter |
__getitem__ |
x.__getitem__(y) <==> x[y] |
__init__(*args, **kwds) |
Create a new, empty Counter object. |
__missing__(key) |
The count of elements not in the Counter is zero. |
__new__((S, ...) |
|
__or__(other) |
Union is the maximum of value in either of the input counters. |
__reduce__() |
|
__reduce_ex__ |
helper for pickle |
__repr__() |
|
__sizeof__(() -> size of D in memory, in bytes) |
|
__sub__(other) |
Subtract count, but keep only results with positive counts. |
__subclasshook__ |
Abstract classes can override this to customize issubclass(). |
clear(() -> None. Remove all items from D.) |
|
copy() |
Return a shallow copy. |
elements() |
Iterator over elements repeating each as many times as its count. |
fromkeys(iterable[, v]) |
|
get((k[,d]) -> D[k] if k in D, ...) |
|
has_key((k) -> True if D has a key k, else False) |
|
items(() -> list of D’s (key, value) pairs, ...) |
|
iteritems(() -> an iterator over the (key, ...) |
|
iterkeys(() -> an iterator over the keys of D) |
|
itervalues(...) |
|
keys(() -> list of D’s keys) |
|
most_common([n]) |
List the n most common elements and their counts from the most common to the least. |
pop((k[,d]) -> v, ...) |
If key is not found, d is returned if given, otherwise KeyError is raised |
popitem(() -> (k, v), ...) |
2-tuple; but raise KeyError if D is empty. |
setdefault((k[,d]) -> D.get(k,d), ...) |
|
subtract(*args, **kwds) |
Like dict.update() but subtracts counts instead of replacing them. |
update(*args, **kwds) |
Like dict.update() but add counts instead of replacing them. |
values(() -> list of D’s values) |
|
viewitems(...) |
|
viewkeys(...) |
|
viewvalues(...) |