-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path23_data_structures.py
More file actions
339 lines (280 loc) · 13.3 KB
/
Copy path23_data_structures.py
File metadata and controls
339 lines (280 loc) · 13.3 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#======================================================================================
# Data Structures
#--------------------------------------------------------------------------------------
# In python, data structures are ways to organize and store data efficiently
# python provides built-in data structures that are easy to use
# Main Data Structures in Python
# List ex:[1,2]
# Tuple ex:(1,2)
# Set ex:{1,2}
# Dictionary ex:{'a':1, 'b':2}
#======================================================================================
#--------------------------------------------------------------------------------------
# Data structure | Ordered | Allows duplicates | Indexed | Mutable
#--------------------------------------------------------------------------------------
# List | Yes | Yes | Yes | Yes
# Tuple | Yes | Yes | Yes | No
# Set | No | No | No | Yes
# Dictionary | Yes | Only for keys | Keyed | Yes
#--------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------
# List --> A list stores multiple items in a single variable.
#--------------------------------------------------------------------------------------
# Features:
# * Ordered
# * Mutable(can change)
# * Allows Duplicate values
#--------------------------------------------------------------------------------------
my_list = [50, 30, 40, 50, 30]
print(my_list) # Ordered, Allow duplicates
print(my_list[1]) # Indexed
my_list[-1] = 10
print(my_list) # Mutable
#--------------------------------------------------------------------------------------
# Tuple --> A tuple is similar to a list but cannot be changed after creation.
#--------------------------------------------------------------------------------------
# Features:
# * Ordered
# * Immutable(unchangeable)
# * Allows duplicates
#--------------------------------------------------------------------------------------
my_tuple = (10, 20, 30, 10, 10)
print(my_tuple) # Ordered, Allows Duplicates
print(my_tuple[0]) # Indexed
#my_tuple[-1] = 20 # Tuple is immutable
print(sorted(my_tuple))
print(type(sorted(my_tuple)) ) # List
#--------------------------------------------------------------------------------------
# Set --> A set stores unordered collection of unique values
#--------------------------------------------------------------------------------------
# Features:
# * Unordered
# * Mutable
# * No duplicates
#--------------------------------------------------------------------------------------
my_set = {10, 20, 30, 40, 20, 20}
print(my_set) # Unordered, Unique
#print(my_set[0]) # Not indexed
my_set.remove(20)
print(my_set) # Mutable
#--------------------------------------------------------------------------------------
# Set Methods
#--------------------------------------------------------------------------------------
# .add(item)
# .update(iterable)
# .remove(item)
# .discard(item)
# .clear()
#--------------------------------------------------------------------------------------
a = {10, 20, 30}
#--------------------------------------------------------------------------------------
# .add() --> inserts the item somewhere in the set, but only if it is new
#--------------------------------------------------------------------------------------
a.add(40)
print(a)
#--------------------------------------------------------------------------------------
# .update() --> merges another group of values(iterable) into the set
#--------------------------------------------------------------------------------------
a.update([50, 60, 70])
print(a)
a.update("HI")
print(a)
a.update({80, 90})
print(a)
#--------------------------------------------------------------------------------------
#NOTE you can use math operators as quick shortcuts: | & - ^
#--------------------------------------------------------------------------------------
# |= works like update for sets
a |= {100, 200, 300}
print(a)
#--------------------------------------------------------------------------------------
# .remove() --> removes an item
#--------------------------------------------------------------------------------------
a.remove(300)
print(a)
# if item does not exist: It throws an error
# a.remove(150) # keyerror
#--------------------------------------------------------------------------------------
# .discard() --> removes the item if it exists and does nothing if it does not
#--------------------------------------------------------------------------------------
a.discard(10)
print(a)
a.discard(1000)
print(a)
#--------------------------------------------------------------------------------------
# .pop() --> pop removes a random item form a set
#--------------------------------------------------------------------------------------
a.pop()
print(a)
#--------------------------------------------------------------------------------------
# .clear() --> removes all items
#--------------------------------------------------------------------------------------
b = {1,3,4,5,9,8}
print(b)
b.clear()
print(b)
print()
#--------------------------------------------------------------------------------------
# Set Math Methods
#--------------------------------------------------------------------------------------
#NOTE: math operator returns a new set and leave the originals untouched
# .union()
# .intersection()
# .difference()
# .symmetric_difference()
#--------------------------------------------------------------------------------------
a = {10, 20, 30, 40, 50}
b = {30, 40, 50, 60, 70}
#--------------------------------------------------------------------------------------
# .union() --> combines all unique items form both sets
#--------------------------------------------------------------------------------------
print(a.union(b))
# shortcut
print(a | b)
#--------------------------------------------------------------------------------------
# .intersection() --> returns only the shared items
#--------------------------------------------------------------------------------------
print(a.intersection(b))
# shortcut
print(a & b)
#--------------------------------------------------------------------------------------
# .difference() --> items in first set but not second
#--------------------------------------------------------------------------------------
print(a.difference(b))
# shortcut
print(a - b) # returns items in 'a', but not in 'b'
print(b - a) # returns items in 'b', but not in 'a'
#--------------------------------------------------------------------------------------
# .symmetric_difference() --> items in both sets except common items
#--------------------------------------------------------------------------------------
print(a.symmetric_difference(b))
# shortcut
print(a ^ b)
# or
print(b ^ a)
print()
#--------------------------------------------------------------------------------------
# Set Relationship Method
#--------------------------------------------------------------------------------------
# .issubset()
# .issuperset()
# .isdisjoint()
#--------------------------------------------------------------------------------------
a = {1,2,3}
b = {6, 5, 3, 1, 4, 2}
#--------------------------------------------------------------------------------------
# .issubset() --> checks if all items of one set exist in another
#--------------------------------------------------------------------------------------
print(a.issubset(b))
#--------------------------------------------------------------------------------------
# .issuperset() --> checks if set contains another set
#--------------------------------------------------------------------------------------
print(b.issuperset(a))
#--------------------------------------------------------------------------------------
# .isdisjoint() --> checks whether sets have no common items
#--------------------------------------------------------------------------------------
a = {1,2}
b = {3,4}
print(a.isdisjoint(b))
#--------------------------------------------------------------------------------------
# Dictionary --> A dictionary stores data in 'key : value' pairs
#--------------------------------------------------------------------------------------
# Features:
# * Ordered (python 3.7+)
# * Mutable
# * keys must be unique
#--------------------------------------------------------------------------------------
my_dict = {'a':10, 'b':20, 'c':20, 'a':40, 'a':50}
print(my_dict) # Ordered, Keys are unique, Values Allow Duplicates
# print(my_dict[1]) # Not Indexed
print(my_dict['a']) # You access values by using their keys, not indexes
my_dict['b'] = 80 # Mutable
print(my_dict)
#--------------------------------------------------------------------------------------
# Dictionary Methods
#--------------------------------------------------------------------------------------
# .get()
# .in operator
# .key()
# .value()
# .items()
# .update()
# .pop()
# .popitem()
# .fromkeys()
#--------------------------------------------------------------------------------------
student = {'name':"Bob", 'age':25, 'marks':90}
# Access
print(student['name'])
#print(student['city']) # If the key is not found, Python throws error
#--------------------------------------------------------------------------------------
# .get() --> # Missing key returns None or your default value
#--------------------------------------------------------------------------------------
print(student.get('city'))
print(student.get('id', "Unknown"))
#--------------------------------------------------------------------------------------
# in operator --> tests if the key is inside the dictionary
#--------------------------------------------------------------------------------------
print('age' in student)
print('name' in student)
print('city' in student)
#--------------------------------------------------------------------------------------
# .key() --> Returns all keys in dictionary
#--------------------------------------------------------------------------------------
print(student.keys())
#--------------------------------------------------------------------------------------
# .value() --> Returns all values in dictionary
#--------------------------------------------------------------------------------------
print(student.values())
#--------------------------------------------------------------------------------------
# .items() --> Returns key-value pairs of your dictionary
#--------------------------------------------------------------------------------------
print(student.items())
print(student)
#--------------------------------------------------------------------------------------
# .items() use cases
# Perfect when you need key and value together
# for looping, transforming data, building new dicts, comparing and more
#--------------------------------------------------------------------------------------
for i in student:
print(i, student[i])
for key, value in student.items():
print(key, value)
#--------------------------------------------------------------------------------------
# .update() --> adds or updates key-value pairs
#--------------------------------------------------------------------------------------
student.update({'city':'mumbai', 'name':"john", 'marks':80, 'email':'john@gmail.com'})
print(student.items())
#--------------------------------------------------------------------------------------
# .pop() --> removes a key from the dictionary and returns its value
#--------------------------------------------------------------------------------------
age = student.pop('age')
print(student)
print(age)
#student.pop('country') # If the key is not found, Python throws a keyerror
#--------------------------------------------------------------------------------------
# .pop() --> removes key and returns its value, or returns your default
# if the key is missing
#--------------------------------------------------------------------------------------
country = student.pop('country', 'Unknown')
print(student)
print(country)
#--------------------------------------------------------------------------------------
# .popitem() --> returns and delete the most recent key value pair from the dictionary
#--------------------------------------------------------------------------------------
rm_pair = student.popitem()
print(student)
print(rm_pair)
#--------------------------------------------------------------------------------------
# .fromkeys() --> builds a new dictionary where all keys get the same default value
#--------------------------------------------------------------------------------------
user = dict.fromkeys(['id', 'name', 'age', 'city'], None)
print(user)
#--------------------------------------------------------------------------------------
# Easy Memory Trick
#--------------------------------------------------------------------------------------
# List --> Editable collection
# Tuple --> Fixed collection
# Set --> Unique values only
# Dictionary --> Key-value data
#--------------------------------------------------------------------------------------