美文网首页
python enumerate()函数

python enumerate()函数

作者: 数据小白周红艳 | 来源:发表于2020-09-28 16:05 被阅读0次

enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。

# 普通的 for 循环
i = 0
seq = ['one', 'two', 'three']
for element in seq:
    print(i,seq[i])
    i+=1
# for 循环使用 enumerate
seq = ['one', 'two', 'three']
for i, element in enumerate(seq):
    print(i,element)
image.png

相关文章

网友评论

      本文标题:python enumerate()函数

      本文链接:https://www.haomeiwen.com/subject/floduktx.html