Python enumerate函数
enumerate()是python的内置函数,用于将一个可遍历的数据对象(如列表、元组或字符串等)组合为一个索引序列,同时列出数据和数据下标,多用于for循环
语法:
enumerate(a,start)
a是可迭代对象,start是计数起始数字
示例一:
li = ["a","b","c","d","e","f"]
for i in enumerate(li):
print(i)
结果:
(0, 'a')
(1, 'b')
(2, 'c')
(3, 'd')
(4, 'e')
(5, 'f')
示例二:
'''
Python大型免费公开课,适合初学者入门
加QQ群:579817333 获取学习资料及必备软件。
'''
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')