​enumerate​​(iterablestart=0)

Return an enumerate object. iterable 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 iterable.

​enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。

​以下是 enumerate() 方法的语法:​

enumerate(sequence, [start=0])

参数

  • sequence -- 一个序列、迭代器或其他支持迭代对象。
  • start -- 下标起始位置的值。

返回值

返回 enumerate(枚举) 对象。

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]

Equivalent to:

def enumerate(sequence, start=0):
n = start
for elem in sequence:
yield n, elem
n += 1

​https://www.runoob.com/python/python-func-enumerate.html​