python enumerate()函数的使用

enumerate(sequence, [start=0])

enumerate()函数一般用在for循环中,参数为序列,迭代器或者是可以迭代的对象。

举个栗子:

list = ['one','two','three']
for index,item in enumerate(list):
    print(index,item)

输出:

0 one
1 two
2 three

指定start后

list = ['one','two','three']
for index,item in enumerate(list, start=1):
    print(index,item)

输出:

1 one
2 two
3 three

发布了22 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/love_withyou/article/details/99698980