简单的来说下:
list = ['a', 'b', 'c']
要求打印出每一个元素的索引和改元素,
一种笨拙的方法:
list = ['a', 'b', 'c']
for i in range(0, len(list)):
print i, list[i]
0 a
1 b
2 c
现在来用enumerate来解决这个问题:
>>> for i, j in enumerate(list):
... print i, j
...
0 a
1 b
2 c
>>>
好了,enumerate的官方解释为:
Return an enumerate object. sequence must be a sequence, an iterator, or some other object which supports iteration. The next() method of the iterator returned by enumerate() returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over sequence
详见:https://docs.python.org/2/library/functions.html#enumerate