【python】浅谈enumerate 函数

时间:2023-03-09 09:32:02
【python】浅谈enumerate 函数

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

>>> 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

>>> for i,j in enumerate('abc',2): ###“2”为第一个元素的坐标
    print i,j

2 a
3 b
4 c