python中itertools包无穷循环器的使用
2017-09-18 20:56
459 查看
无穷循环器
count(5, 2) #从5开始的整数循环器,每次增加2,即5, 7, 9, 11, 13, 15 ...
cycle('abc') #重复序列的元素,既a, b, c, a, b, c ...
repeat(1.2) #重复1.2,构成无穷循环器,即1.2, 1.2, 1.2, ...
repeat也可以有一个次数限制:
repeat(10, 5) #重复10,共重复5次
count(5, 2) #从5开始的整数循环器,每次增加2,即5, 7, 9, 11, 13, 15 ...
cycle('abc') #重复序列的元素,既a, b, c, a, b, c ...
repeat(1.2) #重复1.2,构成无穷循环器,即1.2, 1.2, 1.2, ...
repeat也可以有一个次数限制:
repeat(10, 5) #重复10,共重复5次
>>> import itertools >>> natuals = itertools.count(1) >>> for n in natuals: ... print n ... 1 2 3 ...
>>> import itertools >>> cs = itertools.cycle('ABC') # 注意字符串也是序列的一种 >>> for c in cs: ... print c ... 'A' 'B' 'C' 'A' 'B' 'C' ...
>>> ns = itertools.repeat('A', 10) >>> for n in ns: ... print n ... 打印10次'A'
相关文章推荐
- python itertools的使用
- python itertools的使用
- Python标准库循环器 (itertools)
- Python 内置模块itertools的使用
- Python:itertools模块 combinations和product的使用
- Python标准库13 循环器 (itertools)
- Python itertools的使用
- Python标准库13 循环器 (itertools)
- Python模块 - itertools循环器模块
- Python标准库之循环器(itertools)介绍
- Python模块 - itertools循环器模块
- Python标准库13 循环器 (itertools)
- Python标准库13 循环器 (itertools)学习
- python itertools的使用
- Python标准库13 循环器 (itertools)
- Python小技巧:使用*解包和itertools.product()求笛卡尔积
- Python:itertools模块 combinations和product的使用
- Python标准库之循环器(itertools)介绍
- Python标准库13 循环器 (itertools)
- [转]可爱的 Python:: 使用 itertools 模块中的组合函数