將一個可迭代/遍歷的對象組成一個索引序列,利用他可以同時獲得索引與值
for itm in enumerate(iterable):
print(itm)
# 僅比直接使用for in多一個索引
for i,itm in enumerate(iterable):
print(i, itm)
可以指定索引起始值
>>>list(enumerate('abc'))
# [(0, 'a'), (1, 'b'), (2, 'c')]
# 指定起始值
>>>list(enumerate('abc', 3)) # 從3開始
# [(3, 'a'), (4, 'b'), (5, 'c')]