enumerate是python的内置函数,意思是枚举、列举,用于遍历序列中的元素以及它们的索引。遍历序列的方法大家用的很多,如对一个列表lst = ['a', 'b', 'c', 'd', 'e']进行遍历:
for c in lst:
print(c)
有时候我们在遍历序列的过程中,还需要知道对应元素的索引值,首先能想到的是采用如下的方法:
for i in range(len(lst)):
print(i,lst[i])
稍微繁琐了些,可以直接使用python内置的enumerate函数,提供了更加精简和优雅的写法:
for c in enumerate(lst):
print(c)
输出结果:
(0, 'a')
(1, 'b')
(2, 'c')
(3, 'd')
(4, 'e')
或者:
for i,v in enumerate(lst):
print(i,v)
0 a
1 b
2 c
3 d
4 e
for i, v in enumerate(lst, 101):
print(i, v)
输出结果:
101 a
102 b
103 c
104 d
105 e
直接从指定的数值101开始进行索引编号。