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 i,j in enumerate(('a','b','c')):
 print i,j

 0 a
 1 b
 2 c
 >>> for i,j in enumerate([1,2,3]):
 print i,j

 0 1
 1 2
 2 3
 >>> for i,j in enumerate({'a':1,'b':2}):
 print i,j

 0 a
 1 b
>>> for i,j in enumerate('abc'):
 print i,j

 0 a
 1 b
 2 c

参考资料:
http://blog.sina.com.cn/s/blog_a299c85e0101h3y4.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注