list1 = [1, 2, 3, 4]
我试图找出一种方法来更改每个打印的步骤值
我尝试过的
r = 0 for i in range(0, 10, list1[r]): print i r = r + 1
我建议为此使用while循环实现自己的生成器。范例-
while
def varied_step_range(start,stop,stepiter): step = iter(stepiter) while start < stop: yield start start += next(step)
然后,您可以将其用作-
for i in varied_step_range(start,stop,steplist): #Do your logic.
我们这样step = iter(stepiter)做是stepiter可以进行任何迭代的。
step = iter(stepiter)
stepiter
演示-
>>> def varied_step_range(start,stop,stepiter): ... step = iter(stepiter) ... while start < stop: ... yield start ... start += next(step) ... >>> for i in varied_step_range(0,10,[1,2,3,4]): ... print i ... 0 1 3 6