October 2018
Beginner to intermediate
398 pages
11h 1m
English
Counter is a subclass of a dictionary where each dictionary key is a hashable object and the associated value is an integer count of that object. There are three ways to initialize a counter. We can pass it any sequence object, a dictionary of key:value pairs, or a tuple of the format (object=value,...), as in the following example:
>>> from collections import Counter>>> Counter('anysequence')Counter({'e': 3, 'n': 2, 'a': 1, 'y': 1, 's': 1, 'q': 1, 'u': 1, 'c': 1})>>> c1 = Counter('anysequence')>>> c2= Counter({'a':1, 'c': 1, 'e':3})>>> c3= Counter(a=1, c= 1, e=3)>>> c1Counter({'e': 3, 'n': 2, 'a': 1, 'y': 1, 's': 1, 'q': 1, 'u': 1, 'c': 1})>>> c2Counter({'e': 3, 'a': 1, 'c': 1})>>> c3Counter({'e': 3, 'a': 1, 'c': 1})
Read now
Unlock full access