【4.7】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 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

个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn