标签:for 循环 seq 一个 list spring res pytho art start
enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
>>>seasons = [‘Spring‘, ‘Summer‘, ‘Fall‘, ‘Winter‘]
>>> list(enumerate(seasons))
output:[(0, ‘Spring‘), (1, ‘Summer‘), (2, ‘Fall‘), (3, ‘Winter‘)]
>>> list(enumerate(seasons, start=1))# 下标从 1 开始
output:[(1, ‘Spring‘), (2, ‘Summer‘), (3, ‘Fall‘), (4, ‘Winter‘)]
常用在for循环中:
>>>seq = [‘one‘, ‘two‘, ‘three‘]
>>> for i, element in enumerate(seq):
... print i, element
... 0 one 1 two 2 three
标签:for 循环 seq 一个 list spring res pytho art start
原文地址:https://www.cnblogs.com/qingchen-forever/p/12168532.html