为hashable对象计数,是字典的子类
cnt = collections.Counter(['red', 'blue', 'red', 'green', 'blue', 'blue','blue', 'blue','blue', 'blue'])
class Counter(builtins.dict)
| 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
| >>> sum(c.values()) # total of all counts
| >>> c['a'] # count of letter 'a'
| >>> 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'
| >>> del c['b'] # remove all 'b'
| >>> c['b'] # now there are zero 'b'
| >>> d = Counter('simsalabim') # make another counter
| >>> c.update(d) # add in the second counter
| >>> c['a'] # now there are nine 'a'
| >>> c.clear() # empty the 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)]
| Method resolution order:
| Add counts from two counters.
| >>> Counter('abbb') + Counter('bcc')
| Counter({'b': 4, 'c': 2, 'a': 1})
| Intersection is the minimum of corresponding counts.
| >>> Counter('abbb') & Counter('bcc')
| __delitem__(self, elem)
| Like dict.__delitem__() but does not raise KeyError for missing values.
| Inplace add from another counter, keeping only positive counts.
| >>> c = Counter('abbb')
| >>> c += Counter('bcc')
| Counter({'b': 4, 'c': 2, 'a': 1})
| Inplace intersection is the minimum of corresponding counts.
| >>> c = Counter('abbb')
| >>> c &= Counter('bcc')
| __init__(*args, **kwds)
| Create a new, empty Counter object. And if given, count elements
| from an input iterable. Or, initialize the count from another mapping
| of elements to their counts.
| >>> c = Counter() # a new, empty counter
| >>> c = Counter('gallahad') # a new counter from an iterable
| >>> c = Counter({'a': 4, 'b': 2}) # a new counter from a mapping
| >>> c = Counter(a=4, b=2) # a new counter from keyword args
| Inplace union is the maximum of value from either counter.
| >>> c = Counter('abbb')
| >>> c |= Counter('bcc')
| Counter({'b': 3, 'c': 2, 'a': 1})
| Inplace subtract counter, but keep only results with positive counts.
| >>> c = Counter('abbbc')
| >>> c -= Counter('bccd')
| Counter({'b': 2, 'a': 1})
| The count of elements not in the Counter is zero.
| Subtracts from an empty counter. Strips positive and zero counts,
| and flips the sign on negative counts.
| Union is the maximum of value in either of the input counters.
| >>> Counter('abbb') | Counter('bcc')
| Counter({'b': 3, 'c': 2, 'a': 1})
| Adds an empty counter, effectively stripping negative and zero counts
| Subtract count, but keep only results with positive counts.
| >>> Counter('abbbc') - Counter('bccd')
| Counter({'b': 2, 'a': 1})
| Iterator over elements repeating each as many times as its count.
| >>> c = Counter('ABCABC')
| >>> sorted(c.elements())
| ['A', 'A', 'B', 'B', 'C', 'C']
| # Knuth's example for prime factors of 1836: 2**2 * 3**3 * 17**1
| >>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
| >>> for factor in prime_factors.elements(): # loop over factors
| ... product *= factor # and multiply them
| Note, if an element's count has been set to zero or is a negative
| number, elements() will ignore it.
| most_common(self, n=None)
| List the n most common elements and their counts from the most
| common to the least. If n is None, then list all element counts.
| >>> Counter('abcdeabcdabcaba').most_common(3)
| [('a', 5), ('b', 4), ('c', 3)]
| subtract(*args, **kwds)
| Like dict.update() but subtracts counts instead of replacing them.
| Counts can be reduced below zero. Both the inputs and outputs are
| allowed to contain zero and negative counts.
| Source can be an iterable, a dictionary, or another Counter instance.
| >>> c = Counter('which')
| >>> c.subtract('witch') # subtract elements from another iterable
| >>> c.subtract(Counter('watch')) # subtract elements from another counter
| >>> c['h'] # 2 in which, minus 1 in witch, minus 1 in watch
| >>> c['w'] # 1 in which, minus 1 in witch, minus 1 in watch
| Like dict.update() but add counts instead of replacing them.
| Source can be an iterable, a dictionary, or another Counter instance.
| >>> c = Counter('which')
| >>> c.update('witch') # add elements from another iterable
| >>> d = Counter('watch')
| >>> c.update(d) # add elements from another counter
| >>> c['h'] # four 'h' in which, witch, and watch
| ----------------------------------------------------------------------
| Class methods defined here:
| fromkeys(iterable, v=None) from builtins.type
| Returns a new dict with keys from iterable and values equal to value.
| ----------------------------------------------------------------------
| Data descriptors defined here:
| dictionary for instance variables (if defined)
| list of weak references to the object (if defined)
| ----------------------------------------------------------------------
| Methods inherited from builtins.dict:
| __contains__(self, key, /)
| True if D has a key k, else False.
| __getattribute__(self, name, /)
| Return getattr(self, name).
| x.__getitem__(y) <==> x[y]
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
| __setitem__(self, key, value, /)
| Set self[key] to value.
| D.__sizeof__() -> size of D in memory, in bytes
| D.clear() -> None. Remove all items from D.
| D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.
| D.items() -> a set-like object providing a view on D's items
| D.keys() -> a set-like object providing a view on D's keys
| D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
| If key is not found, d is returned if given, otherwise KeyError is raised
| D.popitem() -> (k, v), remove and return some (key, value) pair as a
| 2-tuple; but raise KeyError if D is empty.
| D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
| D.values() -> an object providing a view on D's values
| ----------------------------------------------------------------------
| Data and other attributes inherited from builtins.dict: