簡(jiǎn)單的來(lái)說(shuō)下:
list = ['a', 'b', 'c']
要求打印出每一個(gè)元素的索引和改元素了赌,
一種笨拙的方法:
list = ['a', 'b', 'c']
for i in range(0, len(list)):
print i, list[i]
0 a
1 b
2 c
現(xiàn)在來(lái)用enumerate來(lái)解決這個(gè)問(wèn)題:
>>> 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
詳見(jiàn):https://docs.python.org/2/library/functions.html#enumerate