Thursday, March 5, 2020

Python collections package

The collections package in Python


Python’s collections package provides various specialized and versatile collection types. This package provides high performance and efficient alternatives to the basic collection types of list, tuple, sets, and dict. The collections package also specifies abstract base classes defining different types of collection functionality like MutableSet and ItemsView

The collections.Counter class


The Counter is a dict subclass that allows you to easily count objects. We can easily find the number of occurrences of any item in the dictionary with different utility methods. The code below constructs an object to count the frequencies of every element passed to the constructor of the Counter.

import collections
freq = collections.Counter(['a','b','a','d'])
print(freq)

Output:

Counter({'a': 2, 'b': 1, 'd': 1})


Count the number of the occurrences of an alphabet in a string



import collections
freq = collections.Counter('python programming')
print('Frequency of all the elements')
print(freq)
print('==the freuency of p==');
print(freq['p'])

Output:
Frequency of all the elements
Counter({'p': 2, 'o': 2, 'n': 2, 'r': 2, 'g': 2, 'm': 2, 'y': 1,
 't': 1, 'h': 1, ' ': 1, 'a': 1, 'i': 1})
==the freuency of p==
2

Count the number of the occurrences of words in a string


import collections
freq = collections.Counter('python is cool programming language 
                   and python is simple too'.split(' '))
print(freq)

Output:
 Counter({'python': 2, 'is': 2, 'cool': 1, 'programming': 1, 'language': 1,
 'and': 1, 'simple': 1, 'too': 1})

OrderedDict 


Python dictionaries are arbitrarily ordered or they are do not maintain the order in which the items are added. We can use the collections.OrderedDict class to create dict objects that maintain the order of the keys. We can construct the OrderedDicts from collections package as the example below, 



#creating ordered dict
from collections import OrderedDict
d=OrderedDict([('Red',2),('Blue',3),('green',5)]);
print(d)

Output:
OrderedDict([('Red', 2), ('Blue', 3), ('green', 5)])


collections.namedtuple 


We can define a new type of Contact using namedtuple as,


from collections import namedtuple
Contact =namedtuple('Contact', ['name', 'city', 'street','mobile'])



There are two arguments, the second argument is the list of attributes that the tuple will contain. We can also write these attributes as either space or comma-separated list.


Contact =namedtuple('Contact', 'name, city, street,mobile')

OR

Contact =namedtuple('Contact', 'name city street mobile')

now, we can create the objects of the named tuple Contact,
like,

contact1=Contact('Raj','Delhi','ABC',7890000000)

For example,

from collections import namedtuple
Contact =namedtuple('Contact', 'name, city, street,mobile')
contact1=Contact('Raj','Delhi','ABC',7890000000)
print('name->'+contact1.name)
print('city->'+contact1.city)
print('mobile->', contact1.mobile)

Output:
name->Raj
city->Delhi
mobile-> 7890000000