Innovate anywhere, anytime withruncode.io Your cloud-based dev studio.
Python

Working with Python Collections Counter

2022-07-19

Working with Counter:

Counter is very useful in counting occurrences of hashable objects. The elements are stored as dictionary keys and counts are stored as dictionary values. 

Example:

To count the frequency of a character in a given string.

>>>  from counter import Counter
    >>>  str = "python is an easy to learn powerful programming language" 
    >>>  c = Counter(str.replace(' ', ''))
           Counter({'a': 6, 'n': 5, 'e': 4, 'g': 4, 'o': 4, 'r': 4, 'l': 3, 'p': 3, 'i': 2, 'm': 2, 's': 2, 'u': 2, 't': 2, 'y': 2, 'h': 1, 'f': 1, 'w': 1})

Initializing Counter:

We can initialize counter in three ways.

>>> import collections
    >>> collections.Counter(['a','b','c','d','e'])
        Counter({'a': 1, 'c': 1, 'b': 1, 'e': 1, 'd': 1})
    >>> collections.Counter({'a':2,'b':1,'c':-2,'d':2,'e':0})
        Counter({'a': 2, 'd': 2, 'b': 1, 'e': 0, 'c': -2})
    >>> collections.Counter(a=2,b=1,c=-2,d=2,e=0)
        Counter({'a': 2, 'd': 2, 'b': 1, 'e': 0, 'c': -2})

Creating and Updating Counters:

>>> import collections
   >>> c = collections.Counter()              # initializing empty counter
   >>> c.update('xyzzzxy')                    # adding elements to empty counter or updating empty counter
   >>> c
       Counter({'z': 3, 'x': 2, 'y': 2})
   >>> c.update({'x': 1, 'z': -1})            # updating empty counter
   >>> c
       Counter({'x': 2, 'y': 2, 'z':2}

elements():

This method is used to get the elements of counter

>>> elements = list(c.elements())
            ['y', 'y', 'x', 'x', 'z', 'z', 'z']
    >>> set(elements)
          {'x', 'y', 'z'}

most_common([n]):

This method is used to get the sequence of n most common elements and their respective counts.

>>> collections.Counter('antidisestablishmentarianism')
           Counter({'i': 5, 'a': 4, 's': 4, 'n': 3, 't': 3, 'e': 2, 'm': 2, 'b': 1, 'd': 1, 'h': 1, 'l': 1, 'r': 1})
    >>> collections.Counter('antidisestablishmentarianism').most_common(3)
       [('i', 5), ('a', 4), ('s', 4)]

we can also count the most_common lines in a file.

>>> with open('filename', 'rb') as f:
        ...    line_count = Counter(f)
        ...    print line_count

Arithmetic Operations:

>>> import collections
    >>> c1 = collections.Counter(['m', 'i', 'c', 'r', 'o', 'i', 'c', 'm', 'm'])
    >>> c2 = collections.Counter('micropyramid')
    >>> c1 + c2
         Counter({'m': 5, 'i': 4, 'c': 3, 'r': 3, 'o': 2, 'a': 1, 'd': 1, 'p': 1, 'y': 1})
    >>> c1 - c2
        Counter({'c': 1, 'm': 1})        # it will not show count zero elements
    >>> c1 & c2
        Counter({'i': 2, 'm': 2, 'c': 1, 'r': 1, 'o': 1})
    >>> c1 | c2
        Counter({'m': 3, 'c': 2, 'i': 2, 'r': 2, 'a': 1, 'd': 1, 'o': 1, 'p': 1, 'y': 1})

Working with OrderedDict:

OrderedDict is the dictionary subclass which tracks the order of its contents in which they are added.

The regular dictionary doesn't track the insertion order, rather it produces the values in an arbirary order.

Regular Dictionary Example:

>>> reg_dict = {}
      >>> reg_dict['x'] = 'X'
      >>> reg_dict['y'] = 'Y'
      >>> reg_dict['z'] = 'Z'
      >>> for k, v in reg_dict.items():
        ...     print k, v
     y Y
     x X
     z Z

      >>> reg_dict2 = {}
      >>> reg_dict2['Z'] = 'Z'
      >>> reg_dict2['y'] = 'Y'
      >>> reg_dict2['x'] = 'X'
      >>> reg_dict2 == reg_dict
        True

 Ordered Dictionary Example:

      >>> ord_dict = {}
      >>> ord_dict['x'] = 'X'
      >>> ord_dict['y'] = 'Y'
      >>> ord_dict['z'] = 'Z'
      >>> for k, v in ord_dict.items():
        ...     print k, v
     x X
     y Y
     z Z

      >>> ord_dict2 = {}
      >>> ord_dict2['Z'] = 'Z'
      >>> ord_dict2['y'] = 'Y'
      >>> ord_dict2['x'] = 'X'
         >>> ord_dict2 == ord_dict
         False

Sorting Dictionary by Key and Value:

>>>  d = {'banana': 3, 'apple':4, 'pear': 1, 'orange': 2}

   >>> OrderedDict(sorted(d.items(), key=lambda t: t[0]))                       # sorting by Key 
          OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])

   >>> OrderedDict(sorted(d.items(), key=lambda t: t[1]))                       # sorting by Value
         OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)])

Working with defaultdict:

defaultdict is a dictionary, unlike regular dictionary defaultdict is initialized with a function called default_factory() it takes no arguments and provides the default value for nonexistent key.

So there is no chance to get KeyError while using defaultdict, perticulary when we deal with nested lists inside a dictionary.

example:

>>> dict = {}
  >>> dict['colours']['favourite'] = 'red'
       KeyError: 'colours'

to solve this problem we should use defaultdict

>>> import collections
    >>> tree = lambda: collections.defaultdict(tree)
    >>> dict = tree()
    >>> dict['colours']['favourite'] = 'red'

to access this using json.dumps

>>> import json
   >>> json.dumps(dict)
      '{"colours": {"favourite": "red"}}'