Introduction to Python Programming for Business and Social Science Applications. Frederick Kaefer
Чтение книги онлайн.
Читать онлайн книгу Introduction to Python Programming for Business and Social Science Applications - Frederick Kaefer страница 23
Figure 3.17 GSS Dictionary Example
Figure 3.18 Output from GSS Dictionary Example
SCU 3.6 Reference a Dictionary
Download the file “SCU 3_6.py” from the companion website and save it either on your computer or on a removable storage device. Open the file in the Python IDLE editor and modify the indicated line of code to print the just the taxi driver’s name from the dictionary. Execute the modified program after the change to verify that the revised code runs and produces the correct result.
Dictionary Operations
We list several commonly used dictionary operations in Table 3.4. We use the first operation listed in the table, “dict,” to construct a dictionary from information specified in several different ways. This is different from just specifying the key-value pairs as we did earlier, because we can use mapping objects as well as data that reside in other sequences of values. The second operation in the table is simply using the dictionary name along with a key value in square brackets to look up the value corresponding to the key value in the dictionary. This may seem like how we have referenced other sequences by using indexes, but there is a clear distinction. The key value does not have to be a number, and even if it is a number, it does not have to follow any specific pattern. The next operation assigns a specified value to the dictionary entry with a specified key value. If that key value already is present in the dictionary, the interpreter updates its corresponding value. If it is not present in the dictionary, the interpreter creates a new entry. The next operation list in the table uses the “del” keyword to remove the specified entry (referenced by a key value) from the dictionary. This operation will result in an exception if there is no entry that has the specified key value. The “get” operation uses the .get() method of the dictionary object to return a value that corresponds to the specified key value in the dictionary. The difference between this and the second operation in the table is that this can specify a default value if there is no matching key value in the dictionary and does not result in an exception if the interpreter cannot find a corresponding key value. The .pop() method is also a method of the dictionary object and is like the .get() method, except that it has the additional feature of removing the entry with the specified key value from the dictionary when it is employed. The .update() method is also a method of the dictionary object, and we use it to make changes to and/or add entries in the dictionary.
Table 3.4
The Python code in Figure 3.19 illustrates the use of several of these dictionary operations using two different dictionary objects. Lines 2 and 3 of the code in Figure 3.19 construct a dictionary based on the GSS data that contains a few specific survey years and the number of respondents from our data set represented in tuples. Storing these data as tuples illustrates that dictionary values can take many forms and has the added advantage of protection against modifying the values due to the fact that tuples are immutable. The code in line 4 creates a second dictionary using the dict operation. The “iterable” that creates this dictionary is a set of three tuples, each being a pair of values that will become key-value pairs in the dictionary. Lines 5 and 6 print out the dictionaries, and the first two lines of output in Figure 3.20 show that the first dictionary has two key-value pairs, where the keys are strings and the values are tuples of three elements. The second dictionary has three key-value pairs where the keys and values are all individual numeric values.
Figure 3.19 Dictionary Operations
Line 8 of the code in Figure 3.19 uses the dictionary .update() method to add the entries from the years dictionary to the gss_respondents dictionary. Line 9 prints out the gss_respondents dictionary, and the labeled line of output shows that there are now five entries in the dictionary (the original two entries along with the three entries from the years dictionary). Line 11 uses a dictionary operation to assign a different value (45) to the dictionary entry with the key value 2014. Line 12 of the code then prints out the dictionary, and the labeled line of output shows the updated value for the year 2014 entry. Line 13 of the code in Figure 3.19 uses the .get() method to retrieve the value for 1992, but as there is not an entry with that key in the dictionary, the output shows that the default value “no value” is printed (and a KeyError exception is avoided). Line 14 uses the .pop() method to retrieve the value for 1991 and reports its value in the output to be 21, and line 15 prints out the dictionary, revealing that there is no longer an entry in the dictionary with a key value 1991. Line 17 of the code uses the .update() method once again to add the values from the years dictionary to the gss_respondents dictionary, and this time two changes occur. The output from printing the dictionary in line 18 shows that from the update operation, the value corresponding to the year 2014 is now 43, and a key value 1991 has been added to the dictionary with a value of 21.
Figure 3.20 Output from Dictionary Operations
SCU 3.7 Dictionary Operations
Download the file “SCU 3_7.py” from the companion website and save it either on your computer or on a removable storage device. Open the file in the Python IDLE editor and modify the indicated line of code to print just the list of the values in the dictionary using the .values() method. Execute the modified program after the change to verify that the revised code runs and produces the correct result.
Lessons learned: In this section,