enumerate()是Python的内置函数,用于将一个可遍历的数据对象(如列表、元组或字符串等)组合为一个索引序列,同时列出数据和数据下标,多用于for循环
语法:
enumerate(a,start)
a是可迭代对象,start是计数起始数字
示例一:
1 li = ["a","b","c","d","e","f"]
2
3 for i in enumerate(li):
4 print(i)
结果:
(0, 'a')
(1, 'b')
(2, 'c')
(3, 'd')
(4, 'e')
(5, 'f')
示例二:
li = ["a","b","c","d","e","f"]
for i in enumerate(li,1):
print(i)
结果:
(1, 'a')
(2, 'b')
(3, 'c')
(4, 'd')
(5, 'e')
(6, 'f')
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx