在python中处理各类序列时,如果我们想显示出这个序列的元素以及它们的下标,可以使用enumerate()函数. enumerate()函数用于遍历用于遍历序列中的元素以及它们的下标,用法如下: 1.参数为一个元组tuple: for index, value in enumerate(('a', 'b', 'c')): '''下标,元素''' print(index,value) 2..参数为一个列表list: for index, value in enumerate([1, 2, 3])
原地址:http://www.newsmth.net/nForum/#!article/Python/95860 最近用到enumerate()函数,于是查了相关的资料. 其中的一篇是这样的:一般情况下,如果要对一个列表或者数组既要遍历索引又要遍历元素时,可以用enumerate 比如: for index,value in enumerate(list): print index,value 当然也可以 for i in range(0,len(list)):
列表是最常用的Python数据类型,前段时间看书的时候,发现了enumerate() 函数非常实用,因为才知道下标可以这么容易的使用,总结一下. class enumerate(object): """ Return an enumerate object. iterable an object supporting iteration The enumerate object yields pairs containing a count (from start, whic