链接:https://docs.python.org/3/library/itertools.html#itertools.cycle
创建一个等步长计数的迭代器
import numpy as np
import itertools as it
start=1
step_size=3.14
counter=it.count(start,step_size)
print([next(counter) for i in range(10)])
结果:
[1, 4.140000000000001, 7.280000000000001, 10.420000000000002, 13.560000000000002, 16.700000000000003, 19.840000000000003, 22.980000000000004, 26.120000000000005, 29.260000000000005]
创建一个环形迭代器
import numpy as np
import itertools as it
cyc=it.cycle([1,2,3])
print([next(cyc) for i in range(10)])
创建一个permutation迭代器
import numpy as np
import itertools as it
cyc=it.permutations(['A','B','C'],2)
print(list(cyc))
结果:
[('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]
|