美文网首页
python中enumerate用法

python中enumerate用法

作者: 李任之 | 来源:发表于2017-04-23 17:44 被阅读0次

简单的来说下:list = ['a', 'b', 'c']
要求打印出每一个元素的索引和改元素,一种笨拙的方法:
list = ['a', 'b', 'c']for i in range(0, len(list)):print i, list[i]0 a1 b2 c

现在来用enumerate来解决这个问题:

for i, j in enumerate(list):... print i, j...0 a1 b2 c>>>

好了,enumerate的官方解释为:
Return an enumerate object. sequence must be a sequence, an iterator, or some other object which supports iteration. The next() method of the iterator returned by enumerate() returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over sequence

详见:https://docs.python.org/2/library/functions.html#enumerate

相关文章

网友评论

      本文标题:python中enumerate用法

      本文链接:https://www.haomeiwen.com/subject/skyyzttx.html