1
+ {
2
+ "metadata" : {
3
+ "name" : " ngs-61-intro-to-dicts"
4
+ },
5
+ "nbformat" : 3 ,
6
+ "worksheets" : [
7
+ {
8
+ "cells" : [
9
+ {
10
+ "cell_type" : " markdown" ,
11
+ "source" : [
12
+ " Goal: take some data from counting birds, and ask questions of it." ,
13
+ " " ,
14
+ " In particular, how many birds did we see of each type?"
15
+ ]
16
+ },
17
+ {
18
+ "cell_type" : " code" ,
19
+ "input" : [
20
+ " s = \"\"\" " ,
21
+ " bluejay" ,
22
+ " goldfinch" ,
23
+ " goldfinch" ,
24
+ " bald eagle" ,
25
+ " ostrich" ,
26
+ " bluejay" ,
27
+ " goose" ,
28
+ " goose" ,
29
+ " goose" ,
30
+ " goose" ,
31
+ " goose" ,
32
+ " goose" ,
33
+ " goose" ,
34
+ " duck" ,
35
+ " \"\"\" "
36
+ ],
37
+ "language" : " python" ,
38
+ "outputs" : [],
39
+ "prompt_number" : 1
40
+ },
41
+ {
42
+ "cell_type" : " markdown" ,
43
+ "source" : [
44
+ " First, let's transform this text list into a \" real\" python list."
45
+ ]
46
+ },
47
+ {
48
+ "cell_type" : " code" ,
49
+ "input" : [
50
+ " birdlist = s.split('\\ n')" ,
51
+ " birdlist = [ x for x in birdlist if x ]" ,
52
+ " birdlist"
53
+ ],
54
+ "language" : " python" ,
55
+ "outputs" : [
56
+ {
57
+ "output_type" : " pyout" ,
58
+ "prompt_number" : 6 ,
59
+ "text" : [
60
+ " ['bluejay'," ,
61
+ " 'goldfinch'," ,
62
+ " 'goldfinch'," ,
63
+ " 'bald eagle'," ,
64
+ " 'ostrich'," ,
65
+ " 'bluejay'," ,
66
+ " 'goose'," ,
67
+ " 'goose'," ,
68
+ " 'goose'," ,
69
+ " 'goose'," ,
70
+ " 'goose'," ,
71
+ " 'goose'," ,
72
+ " 'goose'," ,
73
+ " 'duck']"
74
+ ]
75
+ }
76
+ ],
77
+ "prompt_number" : 6
78
+ },
79
+ {
80
+ "cell_type" : " code" ,
81
+ "input" : [
82
+ " countlist = []" ,
83
+ " for bird in birdlist:" ,
84
+ " newcountlist = []" ,
85
+ " found = False" ,
86
+ " for (j, count) in countlist:" ,
87
+ " if j == bird:" ,
88
+ " found = True" ,
89
+ " count += 1" ,
90
+ " newcountlist.append((j, count))" ,
91
+ " if not found:" ,
92
+ " newcountlist.append((bird, 1))" ,
93
+ " countlist = newcountlist" ,
94
+ " " ,
95
+ " countlist"
96
+ ],
97
+ "language" : " python" ,
98
+ "outputs" : [
99
+ {
100
+ "output_type" : " pyout" ,
101
+ "prompt_number" : 9 ,
102
+ "text" : [
103
+ " [('bluejay', 2)," ,
104
+ " ('goldfinch', 2)," ,
105
+ " ('bald eagle', 1)," ,
106
+ " ('ostrich', 1)," ,
107
+ " ('goose', 7)," ,
108
+ " ('duck', 1)]"
109
+ ]
110
+ }
111
+ ],
112
+ "prompt_number" : 9
113
+ },
114
+ {
115
+ "cell_type" : " markdown" ,
116
+ "source" : [
117
+ " Let's use a dictionary instead"
118
+ ]
119
+ },
120
+ {
121
+ "cell_type" : " code" ,
122
+ "input" : [
123
+ " birddict = {}" ,
124
+ " # set the value associated with the key 'goose' to the number 7" ,
125
+ " birddict['goose'] = 7" ,
126
+ " # get the value associated with the key 'goose'" ,
127
+ " print birddict['goose']"
128
+ ],
129
+ "language" : " python" ,
130
+ "outputs" : [
131
+ {
132
+ "output_type" : " stream" ,
133
+ "stream" : " stdout" ,
134
+ "text" : [
135
+ " 7" ,
136
+ " "
137
+ ]
138
+ }
139
+ ],
140
+ "prompt_number" : 10
141
+ },
142
+ {
143
+ "cell_type" : " code" ,
144
+ "input" : [
145
+ " birddict = {}" ,
146
+ " for bird in birdlist:" ,
147
+ " birddict[bird] = 0" ,
148
+ " " ,
149
+ " for bird in birdlist:" ,
150
+ " birddict[bird] = birddict[bird] + 1" ,
151
+ " " ,
152
+ " birddict"
153
+ ],
154
+ "language" : " python" ,
155
+ "outputs" : [
156
+ {
157
+ "output_type" : " pyout" ,
158
+ "prompt_number" : 14 ,
159
+ "text" : [
160
+ " {'bald eagle': 1," ,
161
+ " 'bluejay': 2," ,
162
+ " 'duck': 1," ,
163
+ " 'goldfinch': 2," ,
164
+ " 'goose': 7," ,
165
+ " 'ostrich': 1}"
166
+ ]
167
+ }
168
+ ],
169
+ "prompt_number" : 14
170
+ },
171
+ {
172
+ "cell_type" : " code" ,
173
+ "input" : [
174
+ " birddict = {}" ,
175
+ " " ,
176
+ " for bird in birdlist:" ,
177
+ " if bird not in birddict:" ,
178
+ " birddict[bird] = 1" ,
179
+ " else:" ,
180
+ " birddict[bird] = birddict[bird] + 1" ,
181
+ " " ,
182
+ " birddict"
183
+ ],
184
+ "language" : " python" ,
185
+ "outputs" : [
186
+ {
187
+ "output_type" : " pyout" ,
188
+ "prompt_number" : 17 ,
189
+ "text" : [
190
+ " {'bald eagle': 1," ,
191
+ " 'bluejay': 2," ,
192
+ " 'duck': 1," ,
193
+ " 'goldfinch': 2," ,
194
+ " 'goose': 7," ,
195
+ " 'ostrich': 1}"
196
+ ]
197
+ }
198
+ ],
199
+ "prompt_number" : 17
200
+ },
201
+ {
202
+ "cell_type" : " code" ,
203
+ "input" : [
204
+ " birddict = {}" ,
205
+ " " ,
206
+ " for bird in birdlist:" ,
207
+ " birddict[bird] = birddict.get(bird, 0) + 1" ,
208
+ " " ,
209
+ " birddict"
210
+ ],
211
+ "language" : " python" ,
212
+ "outputs" : [
213
+ {
214
+ "output_type" : " pyout" ,
215
+ "prompt_number" : 18 ,
216
+ "text" : [
217
+ " {'bald eagle': 1," ,
218
+ " 'bluejay': 2," ,
219
+ " 'duck': 1," ,
220
+ " 'goldfinch': 2," ,
221
+ " 'goose': 7," ,
222
+ " 'ostrich': 1}"
223
+ ]
224
+ }
225
+ ],
226
+ "prompt_number" : 18
227
+ },
228
+ {
229
+ "cell_type" : " code" ,
230
+ "input" : [
231
+ " birddict.keys()"
232
+ ],
233
+ "language" : " python" ,
234
+ "outputs" : [
235
+ {
236
+ "output_type" : " pyout" ,
237
+ "prompt_number" : 19 ,
238
+ "text" : [
239
+ " ['bald eagle', 'bluejay', 'ostrich', 'goose', 'goldfinch', 'duck']"
240
+ ]
241
+ }
242
+ ],
243
+ "prompt_number" : 19
244
+ },
245
+ {
246
+ "cell_type" : " code" ,
247
+ "input" : [
248
+ " birddict.values()"
249
+ ],
250
+ "language" : " python" ,
251
+ "outputs" : [
252
+ {
253
+ "output_type" : " pyout" ,
254
+ "prompt_number" : 20 ,
255
+ "text" : [
256
+ " [1, 2, 1, 7, 2, 1]"
257
+ ]
258
+ }
259
+ ],
260
+ "prompt_number" : 20
261
+ },
262
+ {
263
+ "cell_type" : " code" ,
264
+ "input" : [
265
+ " birddict.items()"
266
+ ],
267
+ "language" : " python" ,
268
+ "outputs" : [
269
+ {
270
+ "output_type" : " pyout" ,
271
+ "prompt_number" : 21 ,
272
+ "text" : [
273
+ " [('bald eagle', 1)," ,
274
+ " ('bluejay', 2)," ,
275
+ " ('ostrich', 1)," ,
276
+ " ('goose', 7)," ,
277
+ " ('goldfinch', 2)," ,
278
+ " ('duck', 1)]"
279
+ ]
280
+ }
281
+ ],
282
+ "prompt_number" : 21
283
+ },
284
+ {
285
+ "cell_type" : " code" ,
286
+ "input" : [
287
+ " x = sorted(birddict.items())" ,
288
+ " x"
289
+ ],
290
+ "language" : " python" ,
291
+ "outputs" : [
292
+ {
293
+ "output_type" : " pyout" ,
294
+ "prompt_number" : 23 ,
295
+ "text" : [
296
+ " [('bald eagle', 1)," ,
297
+ " ('bluejay', 2)," ,
298
+ " ('duck', 1)," ,
299
+ " ('goldfinch', 2)," ,
300
+ " ('goose', 7)," ,
301
+ " ('ostrich', 1)]"
302
+ ]
303
+ }
304
+ ],
305
+ "prompt_number" : 23
306
+ },
307
+ {
308
+ "cell_type" : " code" ,
309
+ "input" : [
310
+ " def get_count(a):" ,
311
+ " return a[1]" ,
312
+ " " ,
313
+ " # Sort by count, not by bird name; use get_count to get the count." ,
314
+ " x = sorted(birddict.items(), key=get_count)" ,
315
+ " # by default sorted goes from least to most; reverse that." ,
316
+ " x.reverse()" ,
317
+ " x"
318
+ ],
319
+ "language" : " python" ,
320
+ "outputs" : [
321
+ {
322
+ "output_type" : " pyout" ,
323
+ "prompt_number" : 33 ,
324
+ "text" : [
325
+ " [('goose', 7)," ,
326
+ " ('goldfinch', 2)," ,
327
+ " ('bluejay', 2)," ,
328
+ " ('duck', 1)," ,
329
+ " ('ostrich', 1)," ,
330
+ " ('bald eagle', 1)]"
331
+ ]
332
+ }
333
+ ],
334
+ "prompt_number" : 33
335
+ },
336
+ {
337
+ "cell_type" : " code" ,
338
+ "input" : [
339
+ " "
340
+ ],
341
+ "language" : " python" ,
342
+ "outputs" : []
343
+ },
344
+ {
345
+ "cell_type" : " code" ,
346
+ "input" : [
347
+ " "
348
+ ],
349
+ "language" : " python" ,
350
+ "outputs" : []
351
+ }
352
+ ]
353
+ }
354
+ ]
355
+ }
0 commit comments