python中的enumerate 函数(编号的实现方式)

enumerate 函数用于遍历序列中的元素以及它们的下标:

默认从0开始,如果想从1开始,可以仿照最后案例 加上逗号,和数字编号

>>> for i,j in enumerate(('a','b','c')):
 print i,j

 
0 a
1 b
2 c
>>> for i,j in enumerate([1,2,3]):
 print i,j

 
0 1
1 2
2 3
>>> for i,j in enumerate({'a':1,'b':2}):
 print i,j

 
0 a
1 b

>>> for i,j in enumerate('abc'):
 print i,j

 
0 a
1 b
2 c

a = ['a','b','c','d','e','f']

for i,j in enumerate(a,1)#此处的1,是以编号1开始,不填写1的话是以0开始

1  a

2  b

3  c

4  d

5  e

6  f

原文地址:https://www.cnblogs.com/pytest/p/9760659.html