Py中enumerate方法【转载】

转自:http://www.runoob.com/python/python-func-enumerate.html

enumerate(sequence, [start=0])
  • sequence -- 一个序列、迭代器或其他支持迭代对象。
  • start -- 下标起始位置。
>>>seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))       # 下标从 1 开始
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]

for循环使用enumerate:

>>>seq = ['one', 'two', 'three']
>>> for i, element in enumerate(seq):
...     print i, element
... 
0 one
1 two
2 three

//在for循环中会返回下标和对应的元素。

原文地址:https://www.cnblogs.com/BlueBlueSea/p/10613474.html