With each loop, the iter_var iteration variable is set to the current ele-
ment of the iterable (sequence, iterator, or object that supports iteration),
presumably for use in suite_to_repeat.

8.5.2 Used with Sequence Types
与序列类型使用

In this section, we will see how the for loop works with the different sequence types. The examples will include string, list, and tuple types.

>>> for eachLetter in 'Names':
... print 'current letter:', eachLetter
...
current letter: N current letter: a current letter: m current letter: e current letter: s
在这一部分,我们将看一下for循环如何与不同的序列类型一起使用,例子会包括字符串、列表和元组类型。
>>> for eachLetter in 'Names':
... print 'current letter:', eachLetter
...
current letter: N current letter: a current letter: m current letter: e current letter: s

When iterating over a string, the iteration variable will always consist of only single characters (strings of length 1). Such constructs may not necessar- ily be useful. When seeking characters in a string, more often than not, the programmer will either use in to test for membership, or one of the string
module functions or string methods to check for substrings.

One place where seeing individual characters does come in handy is dur- ing the debugging of sequences in a for loop in an application where you are expecting strings or entire objects to show up in your print statements. If you see individual characters, this is usually a sign that you received a single string rather than a sequence of objects.
There are three basic ways of iterating over a sequence:

Iterating by Sequence Item
按照序列项迭代

>>> nameList = ['Walter', "Nicole", 'Steven', 'Henry']
>>> for eachName in nameList:
... print eachName, "Lim"
...
Walter Lim Nicole Lim Steven Lim Henry Lim
In the above example, a list is iterated over, and for each iteration, the eachName variable contains the list element that we are on for that particu- lar iteration of the loop.

Iterating by Sequence Index
按照序列索引迭代

An alternative way of iterating through each item is by index offset into the sequence itself:

>>> nameList = ['Cathy', "Terry", 'Joe', 'Heather',
'Lucy']
>>> for nameIndex in range(len(nameList)):
... print "Liu,", nameList[nameIndex]
...
Liu, Cathy Liu, Terry Liu, Joe
Liu, Heather
Liu, Lucy
Rather than iterating through the elements themselves, we are iterating through the indices of the list.
We employ the assistance of the len() built-in function, which provides the total number of elements in the tuple as well as the range() built-in function (which we will discuss in more detail below) to give us the actual sequence to iterate over.

>>> len(nameList)
5
>>> range(len(nameList))
[0, 1, 2, 3, 4]
Using range(), we obtain a list of the indexes that nameIndex iterates over; and using the slice/subscript operator ( [ ] ), we can obtain the corre- sponding sequence element.
Those of you who are performance pundits will no doubt recognize that iteration by sequence item wins over iterating via index. If not, this is some- thing to think about. (See Exercise 8–13.)

Iterating with Item and Index

The best of both worlds comes from using the enumerate() built-in function, which was added to Python in version 2.3. Enough said . . . here is some code:

>>> nameList = ['Donn', 'Shirley', 'Ben', 'Janice',
... 'David', 'Yen', 'Wendy']
>>> for i, eachLee in enumerate(nameList):
... print "%d %s Lee" % (i+1, eachLee)
...
1 Donn Lee
2 Shirley Lee

 

3 Ben Lee
4 Janice Lee
5 David Lee
6 Yen Lee
7 Wendy Lee