python中enumerate函数
enumerate返回一个二元组的可迭代对象,二元组就是元素数为二的元组。
1、字符串
>>> a = "erqwu"
>>> enumerate(a)
<enumerate object at 0x0000018555A1D880>
>>> for i in enumerate(a): ## 可迭代二元组有元素索引和元素值构成
print(i)
(0, 'e')
(1, 'r')
(2, 'q')
(3, 'w')
(4, 'u')
2、
>>> a = ["aaa","bbb","ccc","ddd"]
>>> enumerate(a)
<enumerate object at 0x00000219BE302CC0>
>>> for i in enumerate(a):
print(i)
(0, 'aaa')
(1, 'bbb')
(2, 'ccc')
(3, 'ddd')
3、
>>> a = (399,200,599,2332)
>>> enumerate(a)
<enumerate object at 0x000001AC3D666900>
>>> for i in enumerate(a):
print(i)
(0, 399)
(1, 200)
(2, 599)
(3, 2332)