簡單的來說下:list = ['a', 'b', 'c']
要求打印出每一個元素的索引和改元素续徽,一種笨拙的方法:
list = ['a', 'b', 'c']for i in range(0, len(list)):print i, list[i]0 a1 b2 c
現(xiàn)在來用enumerate來解決這個問題:
for i, j in enumerate(list):... print i, j...0 a1 b2 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