python enumerate 的用法
来源:互联网 发布:mac os x ei系统下载 编辑:程序博客网 时间:2024/05/17 07:36
在同时需要用到index和value值的时候可以用到enumerate,参数为可遍历的变量,如字符串,列表等,返回enumerate类。
例:
import string
s = string.ascii_lowercase
e = enumerate(s)
print s
print list(e)
输出结果为:
>>>
abcdefghijklmnopqrstuvwxyz
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'), (5, 'f'), (6, 'g'), (7, 'h'), (8, 'i'), (9, 'j'), (10, 'k'), (11, 'l'), (12, 'm'), (13, 'n'), (14, 'o'), (15, 'p'), (16, 'q'), (17, 'r'), (18, 's'), (19, 't'), (20, 'u'), (21, 'v'), (22, 'w'), (23, 'x'), (24, 'y'), (25, 'z')]
abcdefghijklmnopqrstuvwxyz
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'), (5, 'f'), (6, 'g'), (7, 'h'), (8, 'i'), (9, 'j'), (10, 'k'), (11, 'l'), (12, 'm'), (13, 'n'), (14, 'o'), (15, 'p'), (16, 'q'), (17, 'r'), (18, 's'), (19, 't'), (20, 'u'), (21, 'v'), (22, 'w'), (23, 'x'), (24, 'y'), (25, 'z')]
再例:
用enumerate将string中的1都找出来,用enumerate实现:
def fun(line):
return ((ids,int(val)) for ids,val in enumerate(line) if val != '0')
print list(fun("000111000111"))
输出结果为:
[(3, 1), (4, 1), (5, 1), (9, 1), (10, 1), (11, 1)]
- python enumerate 的用法
- Python中enumerate的用法
- Python中enumerate的用法
- Python中的enumerate的用法
- Python的enumerate用法总结
- python 函数-enumerate的用法
- Python中enumerate()函数的用法
- python中的enumerate函数的用法
- python中enumerate()函数的用法
- python --enumerate用法
- python中enumerate用法
- python enumerate 用法
- python enumerate 用法
- Python enumerate用法
- python enumerate用法
- python --enumerate用法
- python enumerate 用法
- Python中enumerate用法
- SSH框架中配置Hibernate使用proxool连接池
- 谈谈sizeof这个运算符
- 小程序(一)
- Random Forests原理
- 10^n个整数(亿级)的排序 .
- python enumerate 的用法
- log4j日志等级
- js借助ActiveXObject实现创建文件
- log4cplus
- Swing 线程之SwingUtilities.invokeLater()
- 浏览器页面设置导致的打印位置偏移
- 机房收费系统之上下机
- QTP笔记——代码控制回放模式
- 坐在冬天某个角落里,和你聊聊天~