8.6.6 Sequence-Related Built-in Functions
与序列相关的内建函数


sorted(), reversed(), enumerate(), zip()
Below are some examples of using these loop-oriented sequence-related functions. The reason why they are “sequence-related” is that half of them


(sorted()and zip()) return a real sequence (list), while the other two
(reversed() and enumerate()) return iterators (sequence-like).

>>> albums = ('Poe', 'Gaudi', 'Freud', 'Poe2')
>>> years = (1976, 1987, 1990, 2003)
>>> for album in sorted(albums):
... print album,
...
Freud Gaudi Poe Poe2
>>>
>>> for album in reversed(albums):
... print album,
...
Poe2 Freud Gaudi Poe
>>>
>>> for i, album in enumerate(albums):
... print i, album
...
0 Poe
1 Gaudi
2 Freud
3 Poe2
>>>
>>> for album, yr in zip(albums, years):
... print yr, album
...
1976 Poe
1987 Gaudi
1990 Freud
2003 Poe2

Now that we have covered all the loops Python has to offer, let us take a look at the peripheral commands that typically go together with loops. These include statements to abandon the loop (break) and to immediately begin the next iteration (continue).


8.7 break Statement
break声明

The break statement in Python terminates the current loop and resumes execution at the next statement, just like the traditional break found in C. The most common use for break is when some external condition is trig- gered (usually by testing with an if statement), requiring a hasty exit from a loop. The break statement can be used in both while and for loops.

count = num / 2
while count > 0:


if num % count == 0:
print count, 'is the largest factor of', num
break
count -= 1
The task of this piece of code is to find the largest divisor of a given num- ber num. We iterate through all possible numbers that could possibly be fac- tors of num, using the count variable and decrementing for every value that does not divide num. The first number that evenly divides num is the largest factor, and once that number is found, we no longer need to continue and use break to terminate the loop.

phone2remove = '555-1212'
for eachPhone in phoneList:
if eachPhone == phone2remove:
print "found", phone2remove, '... deleting'
deleteFromPhoneDB(phone2remove)
break
The break statement here is used to interrupt the iteration of the list. The goal is to find a target element in the list, and, if found, to remove it from the database and break out of the loop.


8.8 continue Statement
continue 声明

CORE NOTE: continue statements

Whether in Python, C, Java, or any other structured language that features the continue statement, there is a misconception among some beginning programmers that the traditional continue statement “immediately starts the next iteration of a loop.” While this may seem to be the apparent
action, we would like to clarify this somewhat invalid supposition. Rather
than beginning the next iteration of the loop when a continue statement is encountered, a continue statement terminates or discards the remaining statements in the current loop iteration and goes back to the top. If we are in
a conditional loop, the conditional expression is checked for validity before beginning the next iteration of the loop. Once confirmed, then the next iteration begins. Likewise, if the loop were iterative, a determination must be
made as to whether there are any more arguments to iterate over. Only when
that validation has completed successfully can we begin the next iteration.


The continue statement in Python is not unlike the traditional continue found in other high-level languages. The continue statement can be used in both while and for loops. The while loop is conditional, and the for loop is

 

iterative, so using continue is subject to the same requirements (as high- lighted in the Core Note above) before the next iteration of the loop can begin. Otherwise, the loop will terminate normally.
valid = False count = 3
while count > 0:
input = raw_input("enter password")
# check for valid passwd
for eachPasswd in passwdList:
if input == eachPasswd:
valid = True
break
if not valid: # (or valid == 0)
print "invalid input"
count -= 1
continue else:
break
In this combined example using while, for, if, break, and continue, we are looking at validating user input. The user is given three opportunities to enter the correct password; otherwise, the valid variable remains a false value
of 0, which presumably will result in appropriate action being taken soon after.