码迷,mamicode.com
首页 > 编程语言 > 详细

Python enumerate() 函数

时间:2019-01-20 14:01:00      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:string   元组   迭代   python   语法   reserve   版本   code   print   

 

描述

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

 

Python enumerate() 函数

标签:string   元组   迭代   python   语法   reserve   版本   code   print   

原文地址:https://www.cnblogs.com/Centwei/p/10294279.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!