-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlooping-data-structures lab.py
309 lines (183 loc) · 5.65 KB
/
looping-data-structures lab.py
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
#!/usr/bin/env python
# coding: utf-8
# Use all the new skills you've learned for iterating over data structures like dictionaries and lists to practice in this notebook.
# # Data structures
# The trick is that it is all about state!
# ## Lists
# Lists are easy to encounter and easy to abuse. Lists hold individual items, keeping a specific order. To access them, treat the order like an index. The index starts at `0,` and it continues incrementally every time a new item gets added. A loop (sometimes referred to as *"for loop"*) is the most common operation you can encounter.
# In[2]:
directories = ['Documents', 'Music', 'Desktop', 'Downloads', 'Pictures', 'Movies']
for directory in directories:
print(directory)
# In[3]:
import os
for item in os.listdir('sample_data'):
if os.path.isdir(item):
print("This is a directory {0}".format(item))
else:
print("This is a file: {0}".format(item))
# In[4]:
# Looping is easy, but what about state?
# here state is captured in a new variable called `important_directories`
important_directories = []
for item in os.listdir('.'):
if os.path.isdir(item):
important_directories.append(item)
print(important_directories)
# In[11]:
os.listdir('.')
# In[6]:
important_directories = []
for item in os.listdir('.'):
if item.startswith('.'):
continue # flow control!
if os.path.isdir(item):
important_directories.append(item)
print(important_directories)
# In[7]:
items = ['first', 'second', 'third', 'foo']
#items[-1]
url = "https://colab.research.com/drive/asdfjhasdf/alfredo/oreilly"
parts = url.split('/')
#print(parts)
# Everything except the first three items
#print(parts[3:])
#protocol, _, fqdn = parts[:3]
#print("protocol is: %s" % protocol)
#print(fqdn)
#company = parts[-1]
#print(company)
#print("The first item is: {0}".format(items[0]))
#items[1]
# you can also 'ask' for a given item:
items.index('foo')
# watchout for `ValueError` though!
#items.index('fifth')
# ## Tuples
# Should be treated as "read only" lists, the differences are subtle!
# In[8]:
ro_items = ('first', 'second', 'third')
print("first item in the tuple is: %s" % ro_items.index('first'))
print(ro_items[-1])
for item in ro_items:
print(item)
# In[9]:
# expect an error here, just like a list!
ro_items[9]
# In[ ]:
# same with indexes
ro_items.index('fifth')
# In[12]:
# find out what methods are available in a tuple
for method in dir(tuple()):
if method.startswith('__'):
continue
print(method)
# In[14]:
# tuples are inmmutable
ro_items.append('a')
# ## List Comprehensions
# So easy to abuse!
# In[ ]:
items = ['a', '1', '23', 'b', '4', 'c', 'd']
numeric = []
for item in items:
if item.isnumeric():
numeric.append(item)
print(numeric)
# In[ ]:
# notice the `if` condition at the end, is this more readable? or less?
inlined_numeric = [item for item in items if item.isnumeric()]
inlined_numeric
# In[ ]:
# doubly nested items are usually targetted for list comprehensions
items = ['a', '1', '23', 'b', '4', 'c', 'd']
nested_items = [items, items]
nested_items
# In[ ]:
numeric = []
for parent in nested_items:
for item in parent:
if item.isnumeric():
numeric.append(item)
numeric
# In[ ]:
# and now with list comprehensions
numeric = [item for item in parent for parent in nested_items if item.isnumeric()]
numeric
# In[ ]:
# this can improve readability
numeric = [
item for item in parent
for parent in nested_items
if item.isnumeric()
]
numeric
# ## The awesome dictionary
# One of my favorite data structures in Python, learning it can yield inmense benefits.
# In[ ]:
# dictionaries are mappings, usually referred to as key/value mappings
contacts = {
'alfredo': '+3 678-677-0000',
'noah': '+3 707-777-9191'
}
contacts
# In[ ]:
contacts['noah']
# In[ ]:
# you can get keys as list-like objects
contacts.keys()
# In[ ]:
# or you can get the values as well
contacts.values()
# In[ ]:
# looping over dictionaries default to `.keys()` and you can loop over both keys and values
for key in contacts:
print(key)
for name, phone in contacts.items():
print("Key: {0}, Value: {1}".format(name, phone))
# In[ ]:
# you should treat dictionaries like a small database, with cheap (and fast!) access
contacts['alfredo']
contacts['John']
# In[ ]:
# super nice way to "fallback" when things do not exist
print(contacts.get('John', "Peter"))
try:
contacts['John']
except KeyError:
print("Peter")
# ## Walking the filesystem, inspecting files
# Python has built-in utilities to walk the filesystem. It is a bit clunky, and creating something useful requires stitching things together to produce good output
#
# In[ ]:
import os
# yields the 'current' dir, then the directories, and then any files it finds
# for each level it traverses
for path_info in os.walk('.'):
print(path_info)
break
# In[ ]:
import os
from os.path import abspath, join
# producing absolute paths, instead of a tuple of three items
for top_dir, directories, files in os.walk('.'):
for directory in directories:
print(abspath(join(top_dir, directory)))
for _file in files:
print(abspath(join(top_dir, _file)))
break
# In[ ]:
# Now that absolute paths are shown, we can inspect them for file metadata
import os
from os.path import abspath, join, getsize
sizes = {}
for top_dir, directories, files in os.walk('.'):
for _file in files:
full_path = abspath(join(top_dir, _file))
size = getsize(full_path)
sizes[full_path] = size
#break
sorted_results = sorted(sizes, key=sizes.get, reverse=True)
for path in sorted_results[:10]:
print("Path: {0}, size: {1}".format(path, sizes[path]))