27
loading...
This website collects cookies to deliver better user experience
#Import all container at once
from collections import *
#Import single container
from collections import Counter
# Importing and accessing using alias
import collections as clns
counter = clns.Counter()
from collections import Counter
names = ['George', 'Paul', 'George', 'Ringo', 'John', 'John', 'George', 'John', 'Ringo', 'Paul', 'Ringo', 'Ringo', 'George', 'Paul', 'John', 'John', 'John', 'John', 'John', 'Paul']
names_counter = Counter(names)
print(names_counter)
Output :-
Counter({'John': 8, 'George': 4, 'Paul': 4, 'Ringo': 4})
names_counter.most_common()
Output :-
[('John', 8), ('George', 4), ('Paul', 4), ('Ringo', 4)]
# Regular dict
reg_dict = dict()
reg_dict['apples'] = 3
reg_dict['pineapple'] = 5
reg_dict['mango'] = 1
reg_dict['orange'] = 7
reg_dict['grapes'] = 2
print(f"Regular Dict {reg_dict} \n\n")
from collections import OrderedDict
# Ordered Dict
ord_dict = OrderedDict()
ord_dict['apples'] = 3
ord_dict['pineapple'] = 5
ord_dict['mango'] = 1
ord_dict['orange'] = 7
ord_dict['grapes'] = 2
print(f"Ordered Dict {ord_dict}")
Regular Dict {'apples': 3, 'pineapple': 5, 'mango': 1, 'orange': 7, 'grapes': 2}
Ordered Dict OrderedDict([('apples', 3), ('pineapple', 5), ('mango', 1), ('orange', 7), ('grapes', 2)])
del ord_dict['mango']
print(ord_dict)
Output :-
OrderedDict([('apples', 3), ('pineapple', 5), ('orange', 7), ('grapes', 2)])
ord_dict['mango'] = 11
print(ord_dict)
Output :-
OrderedDict([('apples', 3), ('pineapple', 5), ('orange', 7), ('grapes', 2), ('mango', 11)])
from collections import defaultdict
d_dict = defaultdict(lambda : "Not Available") # passing default value
d_dict['John'] = 11
d_dict['Paul'] = 12
d_dict['George'] = 9
d_dict['Rango'] = 14
print(d_dict['Rango'])
print(d_dict['John'])
print(d_dict['max'])
Output :-
14
11
Not Available
values() :- This function is used to display values of all the dictionaries in ChainMap.
maps() :- This function is used to display keys with corresponding values of all the dictionaries in ChainMap.
from collections import ChainMap
d1 = {'Ram': 12, 'Shyam': 12}
d2 = {'Mango': 3, 'Orange': 4}
d3 = {'Car': 4, 'Bus': 8}
# Defining the chainmap
c = ChainMap(d1, d2, d3)
print(c)
Output :-
ChainMap({'Ram': 12, 'Shyam': 12}, {'Mango': 3, 'Orange': 4}, {'Car': 4, 'Bus': 8})
# printing chainMap using maps
print ("All the ChainMap contents are : ")
print (c.maps)
# printing keys using keys()
print ("All keys of ChainMap are : ")
print (list(c.keys()))
# printing keys using keys()
print ("All values of ChainMap are : ")
print (list(c.values()))
Output :-
All the ChainMap contents are :
[{'Ram': 12, 'Shyam': 12}, {'Mango': 3, 'Orange': 4}, {'Car': 4, 'Bus': 8}]
All keys of ChainMap are :
['Car', 'Bus', 'Mango', 'Orange', 'Ram', 'Shyam']
All values of ChainMap are :
[4, 8, 3, 4, 12, 12]
from collections import namedtuple
Book = namedtuple('Book', ['title', 'author', 'price'])
b = Book('Xyz', 'xyz', 50)
print("accessing using index")
print(b[0])
print(b[1])
print(b[2])
print("\n------------------\n")
print("accessing using name")
print(b.title)
print(b.author)
print(b.price)
print("\n------------------\n")
print("using getattr() ")
print(getattr(b, 'title'))
print(getattr(b, 'author'))
print(getattr(b, 'price'))
Output :-
accessing using index
Xyz
xyz
50
------------------
accessing using name
Xyz
xyz
50
------------------
using getattr()
Xyz
xyz
50
from collections import deque
# initializing deque
dq = deque(['John','Max','Harry','Marcus','Robin','Jackub'])
# using append() to insert element at right end
dq.append("Jimmy")
print ("The deque after appending at right is : ")
print (dq,"\n")
# using appendleft() to insert element at left end
dq.appendleft('Alex')
print ("The deque after appending at left is : ")
print (dq,"\n")
# using pop() to delete element from right end
dq.pop()
print ("The deque after deleting from right is : ")
print (dq,"\n")
# using popleft() to delete element from left end
dq.popleft()
print ("The deque after deleting from left is : ")
print (dq,"\n")
Output :-
The deque after appending at right is :
deque(['John', 'Max', 'Harry', 'Marcus', 'Robin', 'Jackub', 'Jimmy'])
The deque after appending at left is :
deque(['Alex', 'John', 'Max', 'Harry', 'Marcus', 'Robin', 'Jackub', 'Jimmy'])
The deque after deleting from right is :
deque(['Alex', 'John', 'Max', 'Harry', 'Marcus', 'Robin', 'Jackub'])
The deque after deleting from left is :
deque(['John', 'Max', 'Harry', 'Marcus', 'Robin', 'Jackub'])