forked from artofproblemsolving/pythonbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionaries.html
executable file
·979 lines (779 loc) · 39.8 KB
/
dictionaries.html
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>10. Dictionaries — How to Think Like a Computer Scientist: Learning with Python 3 (AoPS Edition)</title>
<link rel="stylesheet" href="_static/style.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/codemirrorEdited.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '1.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript" src="_static/pywindowCodemirrorC.js"></script>
<script type="text/javascript" src="_static/skulpt.min.js"></script>
<script type="text/javascript" src="_static/skulpt-stdlib.js"></script>
<script type="text/javascript" src="_static/aopsmods.js"></script>
<link rel="copyright" title="Copyright" href="copyright.html" />
<link rel="top" title="How to Think Like a Computer Scientist: Learning with Python 3 (AoPS Edition)" href="index.html" />
<link rel="next" title="11. Recursion" href="recursion.html" />
<link rel="prev" title="9. Files" href="files.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="recursion.html" title="11. Recursion"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="files.html" title="9. Files"
accesskey="P">previous</a> |</li>
<li><a href="index.html">How to Think Like a Computer Scientist: Learning with Python 3 (AoPS Edition)</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="body">
<div class="line-block">
<div class="line"><br /></div>
</div>
<div class="section" id="dictionaries">
<h1>10. Dictionaries<a class="headerlink" href="#dictionaries" title="Permalink to this headline">¶</a></h1>
<span class="target" id="index-0"></span><p>All of the compound data types we have studied in detail so far — strings,
lists, and tuples — are sequence types, which use integers as indices to access
the values they contain within them.</p>
<p><strong>Dictionaries</strong> are yet another kind of compound type. They are Python’s
built-in <strong>mapping type</strong>. They map <strong>keys</strong>, which can be any immutable type,
to <strong>values</strong>, which can be any type (heterogeneous), just like the elements
of a list or tuple. In other programming languages, they are called <em>associative
arrays</em> since they associate a key with a value.</p>
<p>As an example, we will create a dictionary to translate English words into
Spanish. For this dictionary, the keys are strings.</p>
<p>One way to create a dictionary is to start with the empty dictionary and add
<strong>key:value pairs</strong>. The empty dictionary is denoted <tt class="docutils literal"><span class="pre">{}</span></tt>:</p>
<div id="dictexampledef" class="pywindow" >
<div id="dictexampledef_code_div" style="display: block">
<textarea rows="3" id="dictexampledef_code" class="active_code" prefixcode="undefined">
eng2sp = {}
eng2sp["one"] = "uno"
eng2sp["two"] = "dos"</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['dictexampledef_code'] = false;
pythonTool.readOnlyFlags['dictexampledef_code'] = true;
</script>
<div id='dictexampledef_error'></div>
<pre id="dictexampledef_suffix" style="display:none">
</pre>
</div>
<p>The first assignment creates a dictionary named <tt class="docutils literal"><span class="pre">eng2sp</span></tt>; the other
assignments add new key:value pairs to the dictionary. We can print the current
value of the dictionary in the usual way:</p>
<div id="dictexampleoutput" class="pywindow" >
<div id="dictexampleoutput_code_div" style="display: block">
<textarea rows="4" id="dictexampleoutput_code" class="active_code" prefixcode="undefined">
eng2sp = {}
eng2sp["one"] = "uno"
eng2sp["two"] = "dos"
print(eng2sp)</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['dictexampleoutput_code'] = true;
pythonTool.readOnlyFlags['dictexampleoutput_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="dictexampleoutput_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="dictexampleoutput_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="dictexampleoutput_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='dictexampleoutput_error'></div>
<div style="text-align: center">
<canvas id="dictexampleoutput_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="dictexampleoutput_suffix" style="display:none">
</pre>
<pre id="dictexampleoutput_pre" class="active_out">
</pre>
<div id="dictexampleoutput_files" class="ac-files ac-files-hidden"></div>
</div>
<p>The key:value pairs of the dictionary are separated by commas. Each pair
contains a key and a value separated by a colon.</p>
<p>Another way to create a dictionary is to provide a list of key:value pairs
using the same syntax as the previous output:</p>
<div id="dictexampleallatonce" class="pywindow" >
<div id="dictexampleallatonce_code_div" style="display: block">
<textarea rows="2" id="dictexampleallatonce_code" class="active_code" prefixcode="undefined">
eng2sp = {"one": "uno", "two": "dos", "three": "tres"}
print(eng2sp)</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['dictexampleallatonce_code'] = true;
pythonTool.readOnlyFlags['dictexampleallatonce_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="dictexampleallatonce_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="dictexampleallatonce_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="dictexampleallatonce_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='dictexampleallatonce_error'></div>
<div style="text-align: center">
<canvas id="dictexampleallatonce_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="dictexampleallatonce_suffix" style="display:none">
</pre>
<pre id="dictexampleallatonce_pre" class="active_out">
</pre>
<div id="dictexampleallatonce_files" class="ac-files ac-files-hidden"></div>
</div>
<p>It doesn’t matter what order we write the pairs. The values in a dictionary are
accessed with keys, not with indices, so there is no need to care about
ordering.</p>
<p>Here is how we use a key to look up the corresponding value:</p>
<div id="dictexamplelookup" class="pywindow" >
<div id="dictexamplelookup_code_div" style="display: block">
<textarea rows="2" id="dictexamplelookup_code" class="active_code" prefixcode="undefined">
eng2sp = {"one": "uno", "two": "dos", "three": "tres"}
print(eng2sp["two"])</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['dictexamplelookup_code'] = true;
pythonTool.readOnlyFlags['dictexamplelookup_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="dictexamplelookup_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="dictexamplelookup_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="dictexamplelookup_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='dictexamplelookup_error'></div>
<div style="text-align: center">
<canvas id="dictexamplelookup_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="dictexamplelookup_suffix" style="display:none">
</pre>
<pre id="dictexamplelookup_pre" class="active_out">
</pre>
<div id="dictexamplelookup_files" class="ac-files ac-files-hidden"></div>
</div>
<p>The key <tt class="docutils literal"><span class="pre">"two"</span></tt> yields the value <tt class="docutils literal"><span class="pre">"dos"</span></tt>.</p>
<p>Lists, tuples, and strings have been called <em>sequences</em>, because their items
occur in order. The dictionary is the first compound type that we’ve
seen that is not a sequence, so we can’t index or slice a dictionary.</p>
<div class="section" id="dictionary-operations">
<span id="index-1"></span><h2>10.1. Dictionary operations<a class="headerlink" href="#dictionary-operations" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="docutils literal"><span class="pre">del</span></tt> statement removes a key:value pair from a dictionary. For example,
the following dictionary contains the names of various fruits and the number of
each fruit in stock:</p>
<div id="dictfruitexample" class="pywindow" >
<div id="dictfruitexample_code_div" style="display: block">
<textarea rows="2" id="dictfruitexample_code" class="active_code" prefixcode="undefined">
inventory = {"apples": 430, "bananas": 312, "oranges": 525, "pears": 217}
print(inventory)</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['dictfruitexample_code'] = true;
pythonTool.readOnlyFlags['dictfruitexample_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="dictfruitexample_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="dictfruitexample_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="dictfruitexample_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='dictfruitexample_error'></div>
<div style="text-align: center">
<canvas id="dictfruitexample_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="dictfruitexample_suffix" style="display:none">
</pre>
<pre id="dictfruitexample_pre" class="active_out">
</pre>
<div id="dictfruitexample_files" class="ac-files ac-files-hidden"></div>
</div>
<p>If someone buys all of the pears, we can remove the entry from the dictionary:</p>
<div id="dictfruitdelexample" class="pywindow" >
<div id="dictfruitdelexample_code_div" style="display: block">
<textarea rows="4" id="dictfruitdelexample_code" class="active_code" prefixcode="undefined">
inventory = {"apples": 430, "bananas": 312, "oranges": 525, "pears": 217}
print(inventory)
del inventory["pears"]
print(inventory) # now there are no pears</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['dictfruitdelexample_code'] = true;
pythonTool.readOnlyFlags['dictfruitdelexample_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="dictfruitdelexample_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="dictfruitdelexample_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="dictfruitdelexample_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='dictfruitdelexample_error'></div>
<div style="text-align: center">
<canvas id="dictfruitdelexample_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="dictfruitdelexample_suffix" style="display:none">
</pre>
<pre id="dictfruitdelexample_pre" class="active_out">
</pre>
<div id="dictfruitdelexample_files" class="ac-files ac-files-hidden"></div>
</div>
<p>Or if we’re expecting more pears soon, we might just change the value
associated with pears:</p>
<div id="dictfruitzeroexample" class="pywindow" >
<div id="dictfruitzeroexample_code_div" style="display: block">
<textarea rows="4" id="dictfruitzeroexample_code" class="active_code" prefixcode="undefined">
inventory = {"apples": 430, "bananas": 312, "oranges": 525, "pears": 217}
print(inventory)
inventory["pears"] = 0
print(inventory) # now there are 0 pears</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['dictfruitzeroexample_code'] = true;
pythonTool.readOnlyFlags['dictfruitzeroexample_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="dictfruitzeroexample_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="dictfruitzeroexample_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="dictfruitzeroexample_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='dictfruitzeroexample_error'></div>
<div style="text-align: center">
<canvas id="dictfruitzeroexample_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="dictfruitzeroexample_suffix" style="display:none">
</pre>
<pre id="dictfruitzeroexample_pre" class="active_out">
</pre>
<div id="dictfruitzeroexample_files" class="ac-files ac-files-hidden"></div>
</div>
<p>A new shipment of bananas arriving could be handled like this:</p>
<div id="dictfruitincexample" class="pywindow" >
<div id="dictfruitincexample_code_div" style="display: block">
<textarea rows="4" id="dictfruitincexample_code" class="active_code" prefixcode="undefined">
inventory = {"apples": 430, "bananas": 312, "oranges": 525, "pears": 217}
print(inventory)
inventory["bananas"] += 200
print(inventory) # now there are more bananas</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['dictfruitincexample_code'] = true;
pythonTool.readOnlyFlags['dictfruitincexample_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="dictfruitincexample_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="dictfruitincexample_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="dictfruitincexample_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='dictfruitincexample_error'></div>
<div style="text-align: center">
<canvas id="dictfruitincexample_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="dictfruitincexample_suffix" style="display:none">
</pre>
<pre id="dictfruitincexample_pre" class="active_out">
</pre>
<div id="dictfruitincexample_files" class="ac-files ac-files-hidden"></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">len</span></tt> function also works on dictionaries; it returns the number
of key:value pairs:</p>
<div id="dictfruitlenexample" class="pywindow" >
<div id="dictfruitlenexample_code_div" style="display: block">
<textarea rows="2" id="dictfruitlenexample_code" class="active_code" prefixcode="undefined">
inventory = {"apples": 430, "bananas": 312, "oranges": 525, "pears": 217}
print(len(inventory))</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['dictfruitlenexample_code'] = true;
pythonTool.readOnlyFlags['dictfruitlenexample_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="dictfruitlenexample_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="dictfruitlenexample_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="dictfruitlenexample_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='dictfruitlenexample_error'></div>
<div style="text-align: center">
<canvas id="dictfruitlenexample_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="dictfruitlenexample_suffix" style="display:none">
</pre>
<pre id="dictfruitlenexample_pre" class="active_out">
</pre>
<div id="dictfruitlenexample_files" class="ac-files ac-files-hidden"></div>
</div>
</div>
<div class="section" id="dictionary-methods">
<h2>10.2. Dictionary methods<a class="headerlink" href="#dictionary-methods" title="Permalink to this headline">¶</a></h2>
<p>Dictionaries have a number of useful built-in methods.</p>
<p>The <tt class="docutils literal"><span class="pre">keys</span></tt> method returns what Python 3 calls a <strong>view</strong> of its underlying keys.
A view object has some similarities to the <tt class="docutils literal"><span class="pre">range</span></tt> object we saw earlier —
it is a lazy promise, to deliver its elements when they’re needed by the
rest of the program. We can iterate over the view, or turn the view into a
list like this:</p>
<div id="dictexamplekeys" class="pywindow" >
<div id="dictexamplekeys_code_div" style="display: block">
<textarea rows="7" id="dictexamplekeys_code" class="active_code" prefixcode="undefined">
eng2sp = {"one": "uno", "two": "dos", "three": "tres"}
for k in eng2sp.keys(): # The order of the k's is not defined
print("Got key "+str(k)+" which maps to value "+str(eng2sp[k]))
ks = list(eng2sp.keys())
print(ks)</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['dictexamplekeys_code'] = true;
pythonTool.readOnlyFlags['dictexamplekeys_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="dictexamplekeys_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="dictexamplekeys_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="dictexamplekeys_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='dictexamplekeys_error'></div>
<div style="text-align: center">
<canvas id="dictexamplekeys_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="dictexamplekeys_suffix" style="display:none">
</pre>
<pre id="dictexamplekeys_pre" class="active_out">
</pre>
<div id="dictexamplekeys_files" class="ac-files ac-files-hidden"></div>
</div>
<p>It is so common to iterate over the keys in a dictionary that we can
omit the <tt class="docutils literal"><span class="pre">keys</span></tt> method call in the <tt class="docutils literal"><span class="pre">for</span></tt> loop — iterating over
a dictionary implicitly iterates over its keys:</p>
<div id="dictloopkeys" class="pywindow" >
<div id="dictloopkeys_code_div" style="display: block">
<textarea rows="4" id="dictloopkeys_code" class="active_code" prefixcode="undefined">
eng2sp = {"one": "uno", "two": "dos", "three": "tres"}
for k in eng2sp: # loop over keys of eng2sp
print("Got key "+str(k)+" which maps to value "+str(eng2sp[k]))</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['dictloopkeys_code'] = true;
pythonTool.readOnlyFlags['dictloopkeys_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="dictloopkeys_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="dictloopkeys_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="dictloopkeys_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='dictloopkeys_error'></div>
<div style="text-align: center">
<canvas id="dictloopkeys_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="dictloopkeys_suffix" style="display:none">
</pre>
<pre id="dictloopkeys_pre" class="active_out">
</pre>
<div id="dictloopkeys_files" class="ac-files ac-files-hidden"></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">values</span></tt> method is similar; it returns a view object which can be turned
into a list:</p>
<div id="dictprintvalues" class="pywindow" >
<div id="dictprintvalues_code_div" style="display: block">
<textarea rows="2" id="dictprintvalues_code" class="active_code" prefixcode="undefined">
eng2sp = {"one": "uno", "two": "dos", "three": "tres"}
print(eng2sp.values())</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['dictprintvalues_code'] = true;
pythonTool.readOnlyFlags['dictprintvalues_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="dictprintvalues_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="dictprintvalues_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="dictprintvalues_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='dictprintvalues_error'></div>
<div style="text-align: center">
<canvas id="dictprintvalues_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="dictprintvalues_suffix" style="display:none">
</pre>
<pre id="dictprintvalues_pre" class="active_out">
</pre>
<div id="dictprintvalues_files" class="ac-files ac-files-hidden"></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">items</span></tt> method also returns a view, which promises a list of tuples — one
tuple for each key:value pair:</p>
<div id="dictprintpairs" class="pywindow" >
<div id="dictprintpairs_code_div" style="display: block">
<textarea rows="2" id="dictprintpairs_code" class="active_code" prefixcode="undefined">
eng2sp = {"one": "uno", "two": "dos", "three": "tres"}
print(eng2sp.items())</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['dictprintpairs_code'] = true;
pythonTool.readOnlyFlags['dictprintpairs_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="dictprintpairs_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="dictprintpairs_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="dictprintpairs_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='dictprintpairs_error'></div>
<div style="text-align: center">
<canvas id="dictprintpairs_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="dictprintpairs_suffix" style="display:none">
</pre>
<pre id="dictprintpairs_pre" class="active_out">
</pre>
<div id="dictprintpairs_files" class="ac-files ac-files-hidden"></div>
</div>
<p>Tuples are often useful for getting both the key and the value at the same
time while we are looping:</p>
<div id="dictforlooppairs" class="pywindow" >
<div id="dictforlooppairs_code_div" style="display: block">
<textarea rows="3" id="dictforlooppairs_code" class="active_code" prefixcode="undefined">
eng2sp = {"one": "uno", "two": "dos", "three": "tres"}
for (k,v) in eng2sp.items():
print("Got "+str(k)+" that maps to "+str(v))</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['dictforlooppairs_code'] = true;
pythonTool.readOnlyFlags['dictforlooppairs_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="dictforlooppairs_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="dictforlooppairs_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="dictforlooppairs_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='dictforlooppairs_error'></div>
<div style="text-align: center">
<canvas id="dictforlooppairs_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="dictforlooppairs_suffix" style="display:none">
</pre>
<pre id="dictforlooppairs_pre" class="active_out">
</pre>
<div id="dictforlooppairs_files" class="ac-files ac-files-hidden"></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">in</span></tt> and <tt class="docutils literal"><span class="pre">not</span> <span class="pre">in</span></tt> operators can test if a key is in the dictionary:</p>
<div id="dictkeytest" class="pywindow" >
<div id="dictkeytest_code_div" style="display: block">
<textarea rows="4" id="dictkeytest_code" class="active_code" prefixcode="undefined">
eng2sp = {"one": "uno", "two": "dos", "three": "tres"}
print("one" in eng2sp) # should be True
print("six" in eng2sp) # should be False
print("tres" in eng2sp) # should be False -- only looks at keys</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['dictkeytest_code'] = true;
pythonTool.readOnlyFlags['dictkeytest_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="dictkeytest_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="dictkeytest_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="dictkeytest_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='dictkeytest_error'></div>
<div style="text-align: center">
<canvas id="dictkeytest_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="dictkeytest_suffix" style="display:none">
</pre>
<pre id="dictkeytest_pre" class="active_out">
</pre>
<div id="dictkeytest_files" class="ac-files ac-files-hidden"></div>
</div>
<p>Looking up a non-existent key in a dictionary causes a runtime error:</p>
<div id="dictkeyerror" class="pywindow" >
<div id="dictkeyerror_code_div" style="display: block">
<textarea rows="2" id="dictkeyerror_code" class="active_code" prefixcode="undefined">
eng2sp = {"one": "uno", "two": "dos", "three": "tres"}
print(eng2sp["dog"]) # will generate an error since "dog" is not a key</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['dictkeyerror_code'] = true;
pythonTool.readOnlyFlags['dictkeyerror_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="dictkeyerror_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="dictkeyerror_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="dictkeyerror_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='dictkeyerror_error'></div>
<div style="text-align: center">
<canvas id="dictkeyerror_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="dictkeyerror_suffix" style="display:none">
</pre>
<pre id="dictkeyerror_pre" class="active_out">
</pre>
<div id="dictkeyerror_files" class="ac-files ac-files-hidden"></div>
</div>
<p>We can get around this error by using the <tt class="docutils literal"><span class="pre">get</span></tt> method. This method will return the associated value if the key is in the dictionary, but we can tell it to return something else if the key is not in the dictionary:</p>
<div id="dictgetexample" class="pywindow" >
<div id="dictgetexample_code_div" style="display: block">
<textarea rows="3" id="dictgetexample_code" class="active_code" prefixcode="undefined">
eng2sp = {"one": "uno", "two": "dos", "three": "tres"}
print(eng2sp.get("three","Not in dictionary!")) # will print three's value
print(eng2sp.get("dog","Not in dictionary!")) # will print the message</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['dictgetexample_code'] = true;
pythonTool.readOnlyFlags['dictgetexample_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="dictgetexample_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="dictgetexample_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="dictgetexample_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='dictgetexample_error'></div>
<div style="text-align: center">
<canvas id="dictgetexample_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="dictgetexample_suffix" style="display:none">
</pre>
<pre id="dictgetexample_pre" class="active_out">
</pre>
<div id="dictgetexample_files" class="ac-files ac-files-hidden"></div>
</div>
</div>
<div class="section" id="aliasing-and-copying">
<span id="index-2"></span><h2>10.3. Aliasing and copying<a class="headerlink" href="#aliasing-and-copying" title="Permalink to this headline">¶</a></h2>
<p>As in the case of lists, because dictionaries are mutable, we need to be
aware of aliasing. Whenever
two variables refer to the same object, changes to one affect the other.</p>
<p>If we want to modify a dictionary and keep a copy of the original, use the
<tt class="docutils literal"><span class="pre">copy</span></tt> method. For example, <tt class="docutils literal"><span class="pre">opposites</span></tt> is a dictionary that contains pairs
of opposites:</p>
<div id="dictaliasexample" class="pywindow" >
<div id="dictaliasexample_code_div" style="display: block">
<textarea rows="3" id="dictaliasexample_code" class="active_code" prefixcode="undefined">
opposites = {"up": "down", "right": "wrong", "yes": "no"}
alias = opposites
copy = opposites.copy() # Shallow copy</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['dictaliasexample_code'] = false;
pythonTool.readOnlyFlags['dictaliasexample_code'] = true;
</script>
<div id='dictaliasexample_error'></div>
<pre id="dictaliasexample_suffix" style="display:none">
</pre>
</div>
<p><tt class="docutils literal"><span class="pre">alias</span></tt> and <tt class="docutils literal"><span class="pre">opposites</span></tt> refer to the same object; <tt class="docutils literal"><span class="pre">copy</span></tt> refers to a
fresh copy of the same dictionary. If we modify <tt class="docutils literal"><span class="pre">alias</span></tt>, <tt class="docutils literal"><span class="pre">opposites</span></tt> is
also changed:</p>
<div id="dictchangedexample" class="pywindow" >
<div id="dictchangedexample_code_div" style="display: block">
<textarea rows="5" id="dictchangedexample_code" class="active_code" prefixcode="undefined">
opposites = {"up": "down", "right": "wrong", "yes": "no"}
alias = opposites
alias["right"] = "left"
print(alias)
print(opposites) # changing the alias also changed the original</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['dictchangedexample_code'] = true;
pythonTool.readOnlyFlags['dictchangedexample_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="dictchangedexample_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="dictchangedexample_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="dictchangedexample_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='dictchangedexample_error'></div>
<div style="text-align: center">
<canvas id="dictchangedexample_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="dictchangedexample_suffix" style="display:none">
</pre>
<pre id="dictchangedexample_pre" class="active_out">
</pre>
<div id="dictchangedexample_files" class="ac-files ac-files-hidden"></div>
</div>
<p>If we modify <tt class="docutils literal"><span class="pre">copy</span></tt>, <tt class="docutils literal"><span class="pre">opposites</span></tt> is unchanged:</p>
<div id="dictunchangedexample" class="pywindow" >
<div id="dictunchangedexample_code_div" style="display: block">
<textarea rows="5" id="dictunchangedexample_code" class="active_code" prefixcode="undefined">
opposites = {"up": "down", "right": "wrong", "yes": "no"}
copy = opposites.copy()
copy["right"] = "left"
print(copy)
print(opposites) # changing the copy did not change the original</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['dictunchangedexample_code'] = true;
pythonTool.readOnlyFlags['dictunchangedexample_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="dictunchangedexample_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="dictunchangedexample_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="dictunchangedexample_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='dictunchangedexample_error'></div>
<div style="text-align: center">
<canvas id="dictunchangedexample_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="dictunchangedexample_suffix" style="display:none">
</pre>
<pre id="dictunchangedexample_pre" class="active_out">
</pre>
<div id="dictunchangedexample_files" class="ac-files ac-files-hidden"></div>
</div>
</div>
<div class="section" id="counting-letters">
<h2>10.4. Counting letters<a class="headerlink" href="#counting-letters" title="Permalink to this headline">¶</a></h2>
<p>Given a string, we might wish to compute a
<strong>frequency table</strong> of the letters in the string — that is, how many times each letter
appears.</p>
<p>Such a frequency table might be useful for compressing a text file. Because different
letters appear with different frequencies, we can compress a file by using
shorter codes for common letters and longer codes for letters that appear less
frequently.</p>
<p>Dictionaries provide an elegant way to generate a frequency table:</p>
<div id="freqtable" class="pywindow" >
<div id="freqtable_code_div" style="display: block">
<textarea rows="4" id="freqtable_code" class="active_code" prefixcode="undefined">
letterCounts = {}
for letter in "Mississippi":
letterCounts[letter] = letterCounts.get(letter, 0) + 1
print(letterCounts)</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['freqtable_code'] = true;
pythonTool.readOnlyFlags['freqtable_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="freqtable_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="freqtable_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="freqtable_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='freqtable_error'></div>
<div style="text-align: center">
<canvas id="freqtable_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="freqtable_suffix" style="display:none">
</pre>
<pre id="freqtable_pre" class="active_out">
</pre>
<div id="freqtable_files" class="ac-files ac-files-hidden"></div>
</div>
<p>We start with an empty dictionary. For each letter in the string, we find the
current count (possibly zero) and increment it. At the end, the dictionary
contains pairs of letters and their frequencies.</p>
<p>It might be more appealing to display the frequency table in alphabetical order. We
can do that with the <tt class="docutils literal"><span class="pre">items</span></tt> and <tt class="docutils literal"><span class="pre">sort</span></tt> methods:</p>
<div id="freqtablesorted" class="pywindow" >
<div id="freqtablesorted_code_div" style="display: block">
<textarea rows="6" id="freqtablesorted_code" class="active_code" prefixcode="undefined">
letterCounts = {}
for letter in "Mississippi":
letterCounts[letter] = letterCounts.get(letter, 0) + 1
letterItems = list(letterCounts.items())
letterItems.sort()
print(letterItems)</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['freqtablesorted_code'] = true;
pythonTool.readOnlyFlags['freqtablesorted_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="freqtablesorted_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="freqtablesorted_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="freqtablesorted_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='freqtablesorted_error'></div>
<div style="text-align: center">
<canvas id="freqtablesorted_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="freqtablesorted_suffix" style="display:none">
</pre>
<pre id="freqtablesorted_pre" class="active_out">
</pre>
<div id="freqtablesorted_files" class="ac-files ac-files-hidden"></div>
</div>
<p>Notice in the first line we had to call the type conversion function <tt class="docutils literal"><span class="pre">list</span></tt>.
That turns the promise we get from <tt class="docutils literal"><span class="pre">items</span></tt> into a list, a step that is
needed before we can use the list’s <tt class="docutils literal"><span class="pre">sort</span></tt> method.</p>
</div>
<div class="section" id="glossary">
<h2>10.5. Glossary<a class="headerlink" href="#glossary" title="Permalink to this headline">¶</a></h2>
<dl class="glossary docutils">
<dt id="term-dictionary">dictionary</dt>
<dd>A collection of key:value pairs that maps from keys to values. The keys
can be any immutable value, and the associated value can be of any type.</dd>
<dt id="term-immutable-data-value">immutable data value</dt>
<dd>A data value which cannot be modified. Assignments to elements or
slices (sub-parts) of immutable values cause a runtime error.</dd>
<dt id="term-key">key</dt>
<dd>A data item that is <em>mapped to</em> a value in a dictionary. Keys are used
to look up values in a dictionary. Each key must be unique
across the dictionary.</dd>
<dt id="term-key-value-pair">key:value pair</dt>
<dd>One of the pairs of items in a dictionary. Values are looked up in a
dictionary by key.</dd>
<dt id="term-mapping-type">mapping type</dt>
<dd>A mapping type is a data type comprised of a collection of keys and
associated values. Python’s only built-in mapping type is the
dictionary. Dictionaries implement the
<a class="reference external" href="http://en.wikipedia.org/wiki/Associative_array">associative array</a>
abstract data type.</dd>
<dt id="term-mutable-data-value">mutable data value</dt>
<dd>A data value which can be modified. The types of all mutable values
are compound types. Lists and dictionaries are mutable; strings
and tuples are not.</dd>
</dl>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="recursion.html" title="11. Recursion"
>next</a> |</li>
<li class="right" >
<a href="files.html" title="9. Files"
>previous</a> |</li>
<li><a href="index.html">How to Think Like a Computer Scientist: Learning with Python 3 (AoPS Edition)</a> »</li>
</ul>
</div>
<div class="footer">
© <a href="copyright.html">Copyright</a> 2014, AoPS Incorporated, 2012, Peter Wentworth, Jeffrey Elkner, Allen B. Downey and Chris Meyers.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.1.
</div>
</body>
</html>