Python enumerate函数
功能:
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 (1, 'a') 2 (2, 'b') 3 (3, 'c') 4 (4, 'd') 5 (5, 'e') 6 (6, 'f')