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, 'a')
(2, 'b')
(3, 'c')
(4, 'd')
(5, 'e')
(6, 'f')

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/30db8f7ac213d7661d22a7b9bda4f76d.html