首页 > 编程语言 > 详细

Py中enumerate方法【转载】

时间:2019-03-28 11:05:43      阅读:171      评论:0      收藏:0      [点我收藏+]

转自:http://www.runoob.com/python/python-func-enumerate.html

enumerate(sequence, [start=0])
  • sequence -- 一个序列、迭代器或其他支持迭代对象。
  • start -- 下标起始位置。
>>>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循环使用enumerate:

>>>seq = [one, two, three]
>>> for i, element in enumerate(seq):
...     print i, element
... 
0 one
1 two
2 three

//在for循环中会返回下标和对应的元素。

Py中enumerate方法【转载】

原文:https://www.cnblogs.com/BlueBlueSea/p/10613474.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!