1
1
Aaron Stevens
4 March 2008
CS108 Lecture 19: Data Collections: Dictionaries
2
CS108 Lecture 19: Data Collections: Dictionaries Aaron Stevens 4 - - PDF document
CS108 Lecture 19: Data Collections: Dictionaries Aaron Stevens 4 March 2008 1 Overview/Questions Review: lists and list operations Associative data relationships, key-value pairs, examples. The Python dictionary ADT. 2 1
1
2
3
4
39.73 40.62 47.83 71.41
5
“BAC” “EQR” “MO” “PCL” 32.51 38.24 23.24 19.32
6
Symbols: Prices:
32.51 38.24 23.24 19.32 “BAC” “EQR” “MO” “PCL”
7
8
9
10
11
Reads a <value> out of a <dict>, assigns to var.
<value> = <dict>[<key>]
x takes on each tuple (key-value pair) in the sequence.
for x in <dict>.items()
x takes on each value in the sequence.
for x in <dict>.values()
Assigns a <value> to be mapped to a <key>.
<dict>[<key>] = <value>
x takes on each key in the sequence.
for x in <dict> for x in <dict>.keys()
12
Deletes the pair corresponding to <key>. del <dict>[<key>] Deletes the entire collection of <key>-<value> pairs. <dict>.clear() Returns the <value> corresponding to <key>. <dict>.get(<key>)
Returns a list of tuple s (<key>, <value>). <dict>.items() Returns a list of all <value> s. <dict>.values() Returns a list of all <key>s. <dict>.keys() Returns True if dictionary contains this key. <dict>.has_key(<key>) <key> in <dict>
13
14
15
16
17
18
19
20