HW1-CS2021 - WordPress.com

advertisement
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
HW1: Exercise in Basic Python data structures.
Reproduce the following python interactive listing using different data of
your choosing, e.g., choose different variable names and data strings other
than ‘Bob Smith’ and ‘Sue Jones’
>>> bob = ['Bob Smith', 42, 30000, 'software']
>>> sue = ['Sue Jones', 45, 40000, 'hardware']
>>> bob[0], sue[2]
('Bob Smith', 40000)
# fetch name, pay
>>> bob[0].split()[-1]
'Smith'
# what's bob's last name?
>>> sue[2] *= 1.25
>>> sue
# give sue a 25% raise
['Sue Jones', 45, 50000.0, 'hardware']
>>> people = [bob, sue ]
# reference in list of lists
>>> for person in people:
print(person)
['Bob Smith', 42, 30000, 'software']
['Sue Jones', 45, 50000.0, 'hardware']
>>> people[1][0]
'Sue Jones'
>>> for person in people:
print(person[0].split()[-1])
person[2] *= 1.20
# print last names
# give each a 20% raise
Smith
Jones
>>> for person in people: print(person[2])
# check new pay
36000.0
60000.0
>>> pays = [person[2] for person in people]
>>> pays
# collect all pay
[36000.0, 60000.0]
>>> pays = map((lambda x: x[2]), people)
3.X)
>>> list(pays)
[36000.0, 60000.0]
# ditto (map is a generator in
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
>>> sum(person[2] for person in people)
built-in
# generator expression, sum
96000.0
>>> people.append(['Tom', 50, 0, None])
>>> len(people)
3
>>> people[-1][0]
'Tom'
>>> NAME, AGE, PAY = range(3)
>>> bob = ['Bob Smith', 42, 10000]
>>> bob[NAME]
# 0, 1, and 2
'Bob Smith'
>>> PAY, bob[PAY]
(2, 10000)
>>> bob = [['name', 'Bob Smith'], ['age', 42], ['pay', 10000]]
>>> sue = [['name', 'Sue Jones'], ['age', 45], ['pay', 20000]]
>>> people = [bob, sue]
>>> for person in people:
print(person[0][1], person[2][1])
# name, pay
Bob Smith 10000
Sue Jones 20000
>>> [person[0][1] for person in people]
# collect names
['Bob Smith', 'Sue Jones']
>>> for person in people:
print(person[0][1].split()[-1])
person[2][1] *= 1.10
# get last names
# give a 10% raise
Smith
Jones
>>> for person in people: print(person[2])
['pay', 11000.0]
['pay', 22000.0]
>>> for person in people:
for (name, value) in person:
if name == 'name': print(value)
Bob Smith
# find a specific field
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
Sue Jones
>>> def field(record, label):
for (fname, fvalue) in record:
if fname == label:
return fvalue
# find any field by name
>>> field(bob, 'name')
'Bob Smith'
>>> field(sue, 'pay')
22000.0
>>> for rec in people:
print(field(rec, 'age'))
# print all ages
42
45
>>> bob = {'name': 'Bob Smith', 'age': 42, 'pay': 30000, 'job': 'dev'}
>>> sue = {'name': 'Sue Jones', 'age': 45, 'pay': 40000, 'job': 'hdw'}
>>> bob['name'], sue['pay']
# not bob[0], sue[2]
('Bob Smith', 40000)
>>> bob['name'].split()[-1]
'Smith'
>>> sue['pay'] *= 1.10
>>> sue['pay']
44000.0
>>> bob = dict(name='Bob Smith', age=42, pay=30000, job='dev')
>>> sue = dict(name='Sue Jones', age=45, pay=40000, job='hdw')
>>> bob
{'pay': 30000, 'job': 'dev', 'age': 42, 'name': 'Bob Smith'}
>>> sue
{'pay': 40000, 'job': 'hdw', 'age': 45, 'name': 'Sue Jones'}
>>>
>>>
>>>
>>>
>>>
>>>
sue = {}
sue['name']
sue['age']
sue['pay']
sue['job']
sue
=
=
=
=
'Sue Jones'
45
40000
'hdw'
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
{'job': 'hdw', 'pay': 40000, 'age': 45, 'name': 'Sue Jones'}
>>> names = ['name', 'age', 'pay', 'job']
>>> values = ['Sue Jones', 45, 40000, 'hdw']
>>> list(zip(names, values))
[('name', 'Sue Jones'), ('age', 45), ('pay', 40000), ('job', 'hdw')]
>>> sue = dict(zip(names, values))
>>> sue
{'job': 'hdw', 'pay': 40000, 'age': 45, 'name': 'Sue Jones'}
>>> fields = ('name', 'age', 'job', 'pay')
>>> record = dict.fromkeys(fields, '?')
>>> record
{'job': '?', 'pay': '?', 'age': '?', 'name': '?'}
>>> bob
{'pay': 30000, 'job': 'dev', 'age': 42, 'name': 'Bob Smith'}
>>> sue
{'job': 'hdw', 'pay': 40000, 'age': 45, 'name': 'Sue Jones'}
>>> people = [bob, sue]
# reference in a list
>>> for person in people:
print(person['name'], person['pay'], sep=', ')
# all pay
Bob Smith, 30000
Sue Jones, 40000
>>> for person in people:
if person['name'] == 'Sue Jones':
print(person['pay'])
# fetch sue's pay
40000
>>> names = [person['name'] for person in people]
>>> names
# collect names
['Bob Smith', 'Sue Jones']
>>> list(map((lambda x: x['name']), people))
# ditto, generate
['Bob Smith', 'Sue Jones']
>>> sum(person['pay'] for person in people)
70000
>>> for person in people:
# sum all pay
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
print(person['name'].split()[-1])
person['pay'] *= 1.10
# last name
# a 10% raise
Smith
Jones
>>> for person in people: print(person['pay'])
33000.0
44000.0
>>> bob2 = {'name':
'age':
'job':
'pay':
{'first': 'Bob', 'last': 'Smith'},
42,
['software', 'writing'],
(40000, 50000)}
>>> bob2['name']
# bob's full name
{'last': 'Smith', 'first': 'Bob'}
>>> bob2['name']['last']
# bob's last name\
'Smith'
>>> bob2['pay'][1]
# bob's upper pay
50000
>>> for job in bob2['job']: print(job)
# all of bob's jobs
software
writing
>> bob2['job'][-1]
# bob's last job
'writing'
>>> bob2['job'].append('janitor')
# bob gets a new job
>>> bob2
{'job': ['software', 'writing', 'janitor'], 'pay': (40000, 50000), 'age': 42,
'name':
{'last': 'Smith', 'first': 'Bob'}}
>>> bob = dict(name='Bob Smith', age=42, pay=30000, job='dev')
>>> sue = dict(name='Sue Jones', age=45, pay=40000, job='hdw')
>>> bob
{'pay': 30000, 'job': 'dev', 'age': 42, 'name': 'Bob Smith'}
>>> db = {}
>>> db['bob'] = bob
>>> db['sue'] = sue
# reference in a dict of dicts
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
>>> db['bob']['name']
# fetch bob's name
'Bob Smith'
>>> db['sue']['pay'] = 50000
>>> db['sue']['pay']
# change sue's pay
# fetch sue's pay
50000
>>> db
{'bob': {'pay': 30000, 'job': 'dev', 'age': 42, 'name': 'Bob Smith'}, 'sue':
{'pay': 50000, 'job': 'hdw', 'age': 45, 'name': 'Sue Jones'}}
>>> import pprint
>>> pprint.pprint(db)
{'bob': {'age': 42, 'job': 'dev', 'name': 'Bob Smith', 'pay': 30000},
'sue': {'age': 45, 'job': 'hdw', 'name': 'Sue Jones', 'pay': 50000}}
>>> for key in db:
print(key, '=>', db[key]['name'])
bob => Bob Smith
sue => Sue Jones
>>> for key in db:
print(key, '=>', db[key]['pay'])
bob => 30000
sue => 50000
>>> for key in db:
print(db[key]['name'].split()[-1])
db[key]['pay'] *= 1.10
Smith
Jones
>>> for record in db.values(): print(record['pay'])
33000.0
55000.0
>>> x = [db[key]['name'] for key in db]
>>> x
['Bob Smith', 'Sue Jones']
>>> x = [rec['name'] for rec in db.values()]
>>> x
['Bob Smith', 'Sue Jones']
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
>>> db['tom'] = dict(name='Tom', age=50, job=None, pay=0)
>>> db['tom']
{'pay': 0, 'job': None, 'age': 50, 'name': 'To'}
>>> db['tom']['name']
'Tom'
>>> list(db.keys())
['bob', 'sue', 'tom']
>>> len(db)
3
>>> [rec['age'] for rec in db.values()
[42, 45, 50]
Download