enumerate(0函数是python内置函数,一般用在for循环中,函数作用:
enumerate()函数用于将一个可遍历的数据对象(如列表、元组或字符串等)组合为一个索引序列,同时列出数据和数据下标。
该函数在python2.3以上版本可用,2.6版本添加start参数.
函数声明:
enumerate(sequence,[start=0])
- 参数 sequence是一个序列、迭代器或其它支持迭代的对象
- 参数 start是下标起始位置的值
- 返回 enumerate枚举对象
使用示例:
seasons = ['Spring','Summer','Fall','Winter']
elems = enumerate(seasons)
lst = list(elems)
print(lst)
elems = enumerate(seasons,start=1)
lst = list(elems)
print(lst)
seq = ['one','two','three']
for i,element in enumerate(seq):
print(i,element)
显示结果:
[(0, ‘Spring’), (1, ‘Summer’), (2, ‘Fall’), (3, ‘Winter’)]
[(1, ‘Spring’), (2, ‘Summer’), (3, ‘Fall’), (4, ‘Winter’)]
0 one
1 two
2 three
全文参考:https://www.runoob.com/python/python-func-enumerate.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/46117.html