Repository navigation
Expand file tree
/
Copy pathpython-collections.py
More file actions
191 lines (147 loc) · 5 KB
/
Copy pathpython-collections.py
File metadata and controls
191 lines (147 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
""" MARKDOWN
---
YamlDesc: CONTENT-ARTICLE
Title: Python Collections
MetaDescription: Python Collections Counter, defaultdict, OrderedDict, deque, ChainMap, namedtuple()
MetaKeywords: Python Collections Counter, defaultdict, OrderedDict, deque, ChainMap, namedtuple()
Author: Venkata Bhattaram / tinitiate.com
ContentName: python-collections
---
MARKDOWN """
""" MARKDOWN
# Python Collections
* Python Collections are data structures to store data, Similar to the built-ins
like list, dict, set, tuple etc. These module based collections provide
additional features. They are part of the **collections** module.
* Python Collections
* **Counter**
* Counter is a dict subclass to count the elements of the dictionary.
* **defaultdict**
* DefaultDict sets a default value for a new key, this enables a dictionary
to have a key with a default value, in the regular dictionary,
we cannot create a key without a value.
* **OrderedDict**
* OrderedDict preserves key order as they are inserted. A regular dict
does not preserve the insertion order, This can be demonstrated when
iterating a dictionary.
* **deque**
* Deque is like a List where in data can be added from either side,
data can be removed from either side and data management need not
be sequential.
* **ChainMap**
* ChainMap is used to encapsulates many dictionaries into one unit.
* **namedtuple()**
* This is a Tuple (immutable) Give Names to each index in the tuple and
values can be assigned to those indexes.
MARKDOWN """
# MARKDOWN ```
################################################################################
# Counter
################################################################################
from collections import Counter
# Count Elements in a String
c = Counter('ABCDABCABA')
print(c)
# Count Words in a Sentence
s = 'Python is cool Java is great SQL is cool and great'
words = s.split()
c = Counter(words)
print(c)
# Count elements in a List
L1 = [1,2,3,4,2,3,4,3,4,4]
c = Counter(L1)
print(c)
# Get Most Common using most_common([n])
# Here we get the Top 2 most common occurances of the List elements
L1 = [1,2,3,4,2,3,4,3,4,4]
c = Counter(L1).most_common(2)
print(c)
# Remove Elements using Subtract
L1 = [1,2,3,4,2,3,4,3,4,4]
c = Counter(L1)
L_Remove = {4:2, 3:1}
c.subtract(L_Remove)
print(c)
# Elements, Is used to convert a Counter to List
L1 = [1,2,3,4,2,3,4,3,4,4]
# Create Counter
c = Counter(L1)
# Use element to convert counter to List
print(sorted(c.elements()))
################################################################################
#Default Dict
################################################################################
from collections import defaultdict
food = defaultdict(lambda: 'Bread')
food['Breakfast'] = 'cereal'
food['Lunch'] = 'Pizza'
print(food['Breakfast'])
print(food['Lunch'])
# Get Value of Key, that doesnt exist
print(food['Dinner'])
################################################################################
#Ordered Dict
################################################################################
from collections import OrderedDict
print("Regular Dict:")
d1 = {}
d1['a'] = 1
d1['b'] = 2
d1['c'] = 3
d1['d'] = 4
d1['e'] = 5
for key, value in d1.items():
print(key, value)
print("Ordered Dict:")
od1 = OrderedDict()
od1['a'] = 1
od1['b'] = 2
od1['c'] = 3
od1['d'] = 4
od1['e'] = 5
for key, value in od1.items():
print(key, value)
################################################################################
#Deque
################################################################################
import collections
# initializing deque
dq = collections.deque([1,2,3])
# Add Element on the Right, using append
dq.append(4)
# Add Element on the left, using appendleft()
dq.appendleft(6)
print (dq)
# Remove Element from Right
dq.pop()
# Remove Element from Left
dq.popleft()
print (dq)
################################################################################
#Chain Map
################################################################################
import collections
# initializing dictionaries
e1 = { 'Emp1' : 1, 'Emp2' : 2 }
e2 = { 'Emp3' : 3, 'Emp4' : 4 }
# initializing ChainMap
chain = collections.ChainMap(e1, e2)
# printing chainMap using maps
print(chain.maps)
# printing keys using keys()
print(list(chain.keys()))
# printing keys using keys()
print(list(chain.values()))
# use new_child() to add new dictionary
e3 = { 'Emp5' : 5, 'Emp6' : 6 }
chain = chain.new_child(e3)
################################################################################
#Named Tuple
################################################################################
from collections import namedtuple
employee = namedtuple('employee', 'name, dept, sal')
e1 = employee('AAA', 'Sales', '1000')
print(e1.name)
print(e1.dept)
print(e1.sal)
# MARKDOWN ```