Читать книгу Introduction to Python Programming for Business and Social Science Applications - Frederick Kaefer - Страница 93
Example Using Tuples and Dictionaries
ОглавлениеThe following example illustrates the usefulness of both tuples and dictionaries in an example using data from the GSS. As described in Chapter 1 when we introduced the GSS data set, many of the fields in the data set are coded. Dictionaries are very useful when working with coded data to look up the meaning of specific codes. The Python code in Figure 3.21 also demonstrates the usefulness of tuples when passing multiple arguments to a function.
Description
Figure 3.21 Tuple and Dictionary Example
The execution of the Python code in Figure 3.21 begins on line 14, as the code that precedes that is a function that we only execute when we invoke it using Python code statements. The code in line 14 creates a tuple of two elements, which are codes corresponding to the data fields “Region” and “Happy” from the GSS. The code in line 15 prints out the value of the variable codes, which is a tuple shown in the first line of output in Figure 3.22. Line 16 calls the code_lookup function with the codes variable as an argument and then assigns the function result to the response variable. It is at this point in the code execution that the flow of control transfers to the code_lookup function starting in line 3. Line 4 unpacks the tuple into two variables, region and happy. Line 5 uses a print statement to display the values of those two variables, shown in the second line of output in Figure 3.22. These variables result from the tuple that the function received as an argument, but they are not tuples themselves but rather individual values. Lines 6 to 9 and 10 to 11 create two dictionaries used in line 12. Line 12 uses a return statement to pass back to the code that called this function two values. The first value is the value found in the region_dict dictionary corresponding to the region key value. The second value is the value in the happy_dict dictionary corresponding to the happy key value. When the flow of control returns to line 16, the interpreter creates a variable (a tuple) through the assignment of the two returned values. This code in line 17 prints out the value of the result variable and in the third line of output in Figure 3.22. Finally, lines 18 and 19 print the individual values from indexing the tuple. Note in line 18 that the index value of 0 references the first value, and in line 19, the index value of 1 references the second value in the tuple. The output in the last two lines of Figure 3.22 shows the results of these two lines of code.
Description
Figure 3.22 Output from Tuple and Dictionary Example
Dictionaries will resurface later in Chapter 5 when we talk more about structured data, and we will see tuples again as some packages have functions or objects with methods that either require parameters to be tuples or return results in tuples.
Lessons learned: In this section, we demonstrated using both tuples and dictionaries alongside each other in our code. We learned that tuples can be very useful for passing multiple values to a function and that dictionaries are very useful with looking up values that correspond to coded data, such as that found in the GSS data set.