python 内置函数enumerate()
enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。在python 3中返回一个生成器,代码如下:
a=["a","b","c","d","e","f","g","h","i"]
print(a)
b=enumerate(a)
print(enumerate(a))
print(list(b))
for m,n in enumerate(a):
print(m,n)
运行结果:
F:\dev\python\python.exe F:/pyCharm/practice/config_dir/zip_demo.py
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
<enumerate object at 0x0000017D24B113F0>
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'), (5, 'f'), (6, 'g'), (7, 'h'), (8, 'i')]
0 a
1 b
2 c
3 d
4 e
5 f
6 g
7 h
8 i
Process finished with exit code 0