Python enumerate()函数

时光总嘲笑我的痴心妄想 提交于 2020-01-16 09:25:37

Python enumerate() 函数
Python 内置函数描述
enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在for循环当中。
Python 2.3. 以上版本可用,2.6 添加start参数。语法
以下是enumerate()方法的语法:
enumerate(sequence, [start=0])参数
sequence -- 一个序列、迭代器或其他支持迭代对象。
start -- 下标起始位置。返回值
返回enumerate(枚举)对象。实例
以下展示了使用enumerate()方法的实例:

>>>seasons = ['Spring', 'Summer', 'Fall', 'Winter'] 
>>> list(enumerate(seasons)) 
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')] 
>>> list(enumerate(seasons, start=1)) # 下标从 1 开始 
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]

#普通的for循环
>>>i = 0 
>>> seq = ['one', 'two', 'three'] 
>>> for element in seq: 
... print i, seq[i] 
... i +=1 ... 
0 one 
1 two 
2 three

#for循环使用enumerate
>>>seq = ['one', 'two', 'three'] 
>>> for i, element in enumerate(seq): 
... print i, element 
... 
0 one 
1 two 
2 three
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!