enumerate字典上是枚举、列举的意思。
C语言中关键字enum也是enumerate的缩写。
python中enumerate方法,返回一个enumerate类型。参数一般是可以遍历的的东西,比如列表,字符串什么的。
python文档中是这么说的:
enumerate(sequence, [start=0])
Return an enumerate object. sequence must be a sequence, an iterator, or some other object which sup-
ports iteration. The next() method of the iterator returned by enumerate() returns a tuple containing
a count (from start which defaults to 0) and the corresponding value obtained from iterating over iter-
able. enumerate() is useful for obtaining an indexed series: (0, seq[0]), (1, seq[1]), (2,
seq[2]), ....
For example:
>>> for i, season in enumerate([’Spring’, ’Summer’, ’Fall’, ’Winter’]):
...
print i, season
0 Spring
1 Summer
2 Fall
3 Winter
我试了一下,如果那句循环改成
for season in [’Spring’, ’Summer’, ’Fall’, ’Winter’]:
其实也是可以的,只是enumerate会返回一个有索引的东西,就是前边的那个i,那个i从0开始,每一项加一。
阅读(15628) | 评论(0) | 转发(2) |