VALORES MÁXIMOS DE VERTIDO
D.- PROTECCION DEL SUELO Y DE LAS AGUAS SUBTERRANEAS
8
9 def modifyElement( element ): 10 element *= 2
11
12 aList = [ 1, 2, 3, 4, 5 ] 13
14 print "Effects of passing entire list:"
15 print "The values of the original list are:"
16
17 for item in aList: 18 print item, 19
20 modifyList( aList ) 21
22 print "\n\nThe values of the modified list are:"
23
24 for item in aList: 25 print item, 26
27 print "\n\nEffects of passing list element:"
28 print "aList[ 3 ] before modifyElement:", aList[ 3 ] 29 modifyElement( aList[ 3 ] )
30 print "aList[ 3 ] after modifyElement:", aList[ 3 ] 31
32 print "\nEffects of passing slices of list:"
33 print "aList[ 2:4 ] before modifyList:", aList[ 2:4 ] 34 modifyList( aList[ 2:4 ] )
35 print "aList[ 2:4 ] after modifyList:", aList[ 2:4 ] Fig. 5.16
Fig. 5.16 Fig. 5.16
Lines 27–30 demonstrate passing a list element (aList[ 3 ], which contains a number, recall that numbers are immutable) to a function. The program first prints the value of aList[ 3 ], which is 8. Then, the program calls function modifyElement (lines 9– 10) passing to parameter element the value 8. Function modifyElement multiplies
element by 2. When the function terminates, the local variable element is destroyed. The value of the original element, aList[ 3 ], in the list is not modified because the value of aList[ 3 ] is immutable. Thus, when control is returned to the main portion of the program, the unmodified value of aList[ 3 ] is printed.
Slicing creates a new sequence; therefore, when a program passes a slice to a function, the original sequence is not affected. Line 33 prints the slice aList[ 2:4 ] to the screen. Line 34 calls function modifyList and passes aList[ 2:4 ]. Line 35 prints the result of calling function modifyList—demonstrating that the original list was not modified.
Notice that function modifyList iterates through its list by accessing the elements using the square bracket operator. If the function contained the code
for item in aList: item *= 2
the list would remain unchanged, because the function would modify the value of local variable item and not the value stored at a particular index in the list.
5.9 Sorting and Searching Lists
Sorting data (i.e., placing the data into a particular order, such as ascending or descending) is a common computing application. For instance, a bank sorts checks by account number to prepare individual monthly bank statements. Telephone companies sort accounts by last names and, within that, by first names, to simplify the search for phone numbers. Almost all organizations sort data—in many cases, massive amounts of data. Sorting data is an in- triguing problem that has attracted some of the most intense research efforts in the field of computer science. In this section, we discuss how to sort a list using list method sort.
Figure 5.17 sorts the values of the 10-element list aList (line 4) into ascending order. Lines 8–9 print the list items. Line 11 calls list method sort—this method sorts the ele- Effects of passing entire list:
The values of the original list are: 1 2 3 4 5
The values of the modified list are: 2 4 6 8 10
Effects of passing list element: aList[ 3 ] before modifyElement: 8 aList[ 3 ] after modifyElement: 8 Effects of passing slices of list: aList[ 2:4 ] before modifyList: [6, 8] aList[ 2:4 ] after modifyList: [6, 8]
Fig. 5.16 Fig. 5.16 Fig. 5.16
ments of aList in ascending order. The remainder of the program prints the results of sorting the list.
Much research has been performed in the area of list-sorting algorithms, resulting in the design of many algorithms. Some of these algorithms are simple to express and pro- gram, but are inefficient. Other algorithms are complex and sophisticated, but provide increased performance. The exercises at the end of this chapter investigate a well-known sorting algorithm.
Performance Tip 5.1
Sometimes, the simplest algorithms perform poorly. Their virtue is that they are easy to write, test and debug. Sometimes complex algorithms are needed to realize maximum per-
formance. 5.1
Often, programmers work with large amounts of data stored in lists. It might be neces- sary to determine whether a list contains a value that matches a certain key value. The pro- cess of locating a particular element value in a list is called searching.
The program in Fig. 5.18 searches a list for a value. Line 5 creates list aList, which contains the even numbers between 0 and 198, inclusive. Line 7 then retrieves the search key from the user and assigns the value to variable searchKey. Keyword in tests whether list aList contains the user-entered search key (line 9). If the list contains the value stored in variable searchKey, the expression (line 9) evaluates to true; otherwise, the expression evaluates to false.
1 # Fig. 5.17: fig05_17.py 2 # Sorting a list.
3
4 aList = [ 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 ] 5
6 print "Data items in original order"
7
8 for item in aList: 9 print item, 10
11 aList.sort() 12
13 print "\n\nData items after sorting"
14
15 for item in aList: 16 print item, 17
18 print
Data items in original order 2 6 4 8 10 12 89 68 45 37 Data items after sorting 2 4 6 8 10 12 37 45 68 89
Fig. 5.17 Fig. 5.17 Fig. 5.17
If the list contains the search key, line 10 invokes list method index to obtain the index of the search key. List method index takes a search key as a parameter, searches through the list and returns the index of the first list value that matches the search key. If the list does not contain any value that matches the search key, the program displays an error message. [Note: Figure 5.18 searches aList twice (lines 9–10), which, for large sequences, can result in poor performance. To improve performance, the program can use list method index and trap the exception that occurs if the argument is not in the list. We discuss exception-handling techniques in Chapter 12.]
As with sorting, a great deal of research has been devoted to the task of searching. In the exercises at the end of this chapter, we explore some of the more sophisticated ways of searching a list.
5.10 Multiple-Subscripted Sequences
Sequences can contain elements that are also sequences (i.e., lists and tuples). Such se- quences have multiple subscripts. A common use of multiple-subscripted sequences is to represent tables of values consisting of information arranged in rows and columns. To iden- tify a particular table element, we must specify two subscripts—by convention, the first identifies the element’s row, the second the element’s column.
Sequences that require two subscripts to identify a particular element are called double-subscripted sequences or two-dimensional sequences. Note that multiple-sub- scripted sequences can have more than two subscripts. Python does not support multiple- subscripted sequences directly, but allows programmers to specify single-subscripted tuples and lists whose elements are also single-subscripted tuples and lists, thus achieving the same effect. Figure 5.19 illustrates a double-subscripted sequence, a, containing three rows and four columns (i.e., a 3-by-4 sequence). In general, a sequence with m rows and n columns is called an m-by-n sequence.
1 # Fig. 5.18: fig05_18.py
2 # Searching a list for an integer. 3
4 # Create a list of even integers 0 to 198 5 aList = range( 0, 199, 2 )
6
7 searchKey = int( raw_input( "Enter integer search key: " ) ) 8
9 if searchKey in aList:
10 print "Found at index:", aList.index( searchKey ) 11 else: