python enumerate 用法
2012-03-02 21:59
507 查看
python enumerate 用法 [Python俱乐部]
返回值为enumerate类:
python enumerate 用法
python enumerate 用法 | 在for循环中得到计数
参数为可遍历的变量,如 字符串,列表等;返回值为enumerate类:
import string s = string.ascii_lowercase e = enumerate(s) print s print list(e)输出为:
abcdefghij [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'), (5, 'f'), (6, 'g'), (7, 'h'), (8, 'i'), (9, 'j')]在同时需要index和value值的时候可以使用 enumerate。
enumerate 实战
line 是个 string 包含 0 和 1,要把1都找出来:#方法一 def read_line(line): sample = {} n = len(line) for i in range(n): if line[i]!='0': sample[i] = int(line[i]) return sample #方法二 def xread_line(line): return((idx,int(val)) for idx, val in enumerate(line) if val != '0') print read_line('0001110101') print list(xread_line('0001110101'))
相关文章推荐
- Python的enumerate用法总结
- python enumerate 用法总结
- python enumerate用法
- python天天进步(7)--enumerate用法
- python中enumerate的用法实例解析
- python(31) 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 用法