Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 4 ■ DICTIONARIES: WHEN INDICES WON’T DO 71 people = { } 'Alice': { 'phone': '2341', 'addr': 'Foo drive 23' }, 'Beth': { 'phone': '9102', 'addr': 'Bar street 42' }, 'Cecil': { 'phone': '3158', 'addr': 'Baz avenue 90' } # Descriptive labels for the phone number and address. These will be used # when printing the output. labels = { 'phone': 'phone number', 'addr': 'address' } name = raw_input('Name: ') # Are we looking for a phone number or an address? request = raw_input('Phone number (p) or address (a)? ') # Use the correct key: if request == 'p': key = 'phone' if request == 'a': key = 'addr' # Only try to print information if the name is a valid key in our dictionary: if name in people: print "%s's %s is %s." % (name, labels[key], people[name][key]) String Formatting with Dictionaries In Chapter 3, you saw how you could use string formatting to format all the values in a tuple. If you use a dictionary (with only strings as keys) instead of a tuple, you can make the string formatting even snazzier. After the % character in each conversion specifier, you add a key (enclosed in parentheses), which is followed by the other specifier elements:
72 CHAPTER 4 ■ DICTIONARIES: WHEN INDICES WON’T DO >>> phonebook {'Beth': '9102', 'Alice': '2341', 'Cecil': '3258'} >>> "Cecil's phone number is %(Cecil)s." % phonebook "Cecil's phone number is 3258." Except for the added string key, the conversion specifiers work as before. When using dictionaries like this, you may have any number of conversion specifiers, as long as all the given keys are found in the dictionary. This sort of string formatting can be very useful in template systems (in this case using HTML): >>> template = ''' %(title)s %(title)s %(text)s ''' >>> data = {'title': 'My Home Page', 'text': 'Welcome to my home page!'} >>> print template % data My Home Page My Home Page Welcome to my home page! ■Note The string.Template class (mentioned in Chapter 3) is also quite useful for this kind of application. Dictionary Methods Just like the other built-in types, dictionaries have methods. While these methods can be very useful, you probably will not need them as often as the list and string methods. You might want to skim this section first to get a picture of which methods are available, and then come back later if you need to find out exactly how a given method works. clear The clear method removes all items from the dictionary. This is an in-place operation (like list.sort), so it returns nothing (or, rather, None): >>> d = {} >>> d['name'] = 'Gumby' >>> d['age'] = 42 >>> d {'age': 42, 'name': 'Gumby'}
- Page 52 and 53: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 54 and 55: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 56 and 57: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 58 and 59: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 60 and 61: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 62 and 63: CHAPTER 2 ■ ■ ■ Lists and Tup
- Page 64 and 65: CHAPTER 2 ■ LISTS AND TUPLES 33 I
- Page 66 and 67: CHAPTER 2 ■ LISTS AND TUPLES 35 A
- Page 68 and 69: CHAPTER 2 ■ LISTS AND TUPLES 37 >
- Page 70 and 71: CHAPTER 2 ■ LISTS AND TUPLES 39 M
- Page 72 and 73: CHAPTER 2 ■ LISTS AND TUPLES 41 T
- Page 74 and 75: CHAPTER 2 ■ LISTS AND TUPLES 43 L
- Page 76 and 77: CHAPTER 2 ■ LISTS AND TUPLES 45 W
- Page 78 and 79: CHAPTER 2 ■ LISTS AND TUPLES 47
- Page 80 and 81: CHAPTER 2 ■ LISTS AND TUPLES 49 a
- Page 82 and 83: CHAPTER 2 ■ LISTS AND TUPLES 51 S
- Page 84 and 85: CHAPTER 3 ■ ■ ■ Working with
- Page 86 and 87: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 88 and 89: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 90 and 91: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 92 and 93: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 94 and 95: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 96 and 97: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 98 and 99: CHAPTER 4 ■ ■ ■ Dictionaries:
- Page 100 and 101: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 104 and 105: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 106 and 107: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 108 and 109: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 110: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 113 and 114: 82 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 115 and 116: 84 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 117 and 118: 86 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 119 and 120: 88 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 121 and 122: 90 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 123 and 124: 92 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 125 and 126: 94 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 127 and 128: 96 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 129 and 130: 98 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 131 and 132: 100 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 133 and 134: 102 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 135 and 136: 104 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 137 and 138: 106 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 139 and 140: 108 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 141 and 142: 110 CHAPTER 6 ■ ABSTRACTION But w
- Page 143 and 144: 112 CHAPTER 6 ■ ABSTRACTION ■Ti
- Page 145 and 146: 114 CHAPTER 6 ■ ABSTRACTION Can I
- Page 147 and 148: 116 CHAPTER 6 ■ ABSTRACTION stora
- Page 149 and 150: 118 CHAPTER 6 ■ ABSTRACTION 4. Yo
- Page 151 and 152: 120 CHAPTER 6 ■ ABSTRACTION The p
72 CHAPTER 4 ■ DICTIONARIES: WHEN INDICES WON’T DO<br />
>>> phonebook<br />
{'Beth': '9102', 'Alice': '2341', 'Cecil': '3258'}<br />
>>> "Cecil's phone number is %(Cecil)s." % phonebook<br />
"Cecil's phone number is 3258."<br />
Except for the added string key, the conversion specifiers work as before. When using<br />
dictionaries like this, you may have any number of conversion specifiers, as long as all the<br />
given keys are found in the dictionary. This sort of string formatting can be very useful in<br />
template systems (in this case using HTML):<br />
>>> template = '''<br />
%(title)s<br />
<br />
%(title)s<br />
%(text)s<br />
'''<br />
>>> data = {'title': 'My Home Page', 'text': 'Welcome <strong>to</strong> my home page!'}<br />
>>> print template % data<br />
<br />
My Home Page<br />
<br />
My Home Page<br />
Welcome <strong>to</strong> my home page!<br />
<br />
■Note The string.Template class (mentioned in Chapter 3) is also quite useful for this kind of application.<br />
Dictionary Methods<br />
Just like the other built-in types, dictionaries have methods. While these methods can be very<br />
useful, you probably will not need them as often as the list and string methods. You might want<br />
<strong>to</strong> skim this section first <strong>to</strong> get a picture of which methods are available, and then come back<br />
later if you need <strong>to</strong> find out exactly how a given method works.<br />
clear<br />
The clear method removes all items from the dictionary. This is an in-place operation (like<br />
list.sort), so it returns nothing (or, rather, None):<br />
>>> d = {}<br />
>>> d['name'] = 'Gumby'<br />
>>> d['age'] = 42<br />
>>> d<br />
{'age': 42, 'name': 'Gumby'}