更改 Python 循环中的步骤

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46179757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:31:28  来源:igfitidea点击:

Changing step in Python loop

python

提问by David

In Python 2.7 I want to modify the step of a forloop in function of the specifics conditions satisfied in the loop. Something like this:

在 Python 2.7 中,我想在for循环中满足特定条件的函数中修改循环的步骤。像这样的东西:

step = 1
for i in range(1, 100, step):
    if ...... :
        step = 1
        #do stuff
    else:
        step = 2
        #do other stuff

but it seems that this can't be done, stepis always 1.

但似乎这不能做到,step总是1。

Thanks.

谢谢。

回答by OLIVER.KOO

you would need to increment stepmanually which can be done using a whileloop. checkout difference between whileand forloop.

您需要step手动递增,这可以使用while循环来完成。loop之间的结帐差异whilefor

The for statement iterates through a collection or iterable object or generator function.

The while statement simply loops until a condition is False.

for 语句遍历集合或可迭代对象或生成器函数。

while 语句只是循环,直到条件为 False。

if you use a whileloop your code would look something like this:

如果您使用while循环,您的代码将如下所示:

step = 1
i = 1
while i < 100:
    if ...... :
        step = 1
        #do stuff
    else:
        step = 2
        #do other stuff
    i = i + step

回答by Nenad

You could do it with a while loop:

你可以用一个while循环来做到这一点:

step = 1
i = 1
while i < 100:
    if ...... :
        step = 1
        #do stuff
    else:
        step = 2
        #do other stuff
    i+=step

回答by Jared Goguen

If you want to over-complicate things, you could create a custom generator where you can use the generator.sendmethod to pass in a new step during iteration.

如果您想让事情变得过于复杂,您可以创建一个自定义生成器,您可以在其中使用该generator.send方法在迭代过程中传入一个新步骤。

def variable_step_generator(start, stop=None, step=1):
    if stop is None:
        start, stop = 0, start

    while start < stop:
        test_step = yield start

        if test_step is not None:
            step = test_step
            yield

        start += step

With usage like:

用法如下:

variable_step_range = variable_step_generator(1, 100)
for i in variable_step_range:
    print i
    if i == 10:
        variable_step_range.send(10)
    if i == 90:
        variable_step_range.send(1)

#  1,  2,  3,  4,  5,  6,  7,  8,  9, 
# 10, 20, 30, 40, 50, 60, 70, 80, 90, 
# 91, 92, 93, 94, 95, 96, 97, 98, 99

But this isn't really much more than a wrapper around the whileloop that the other answers suggest.

但这实际上只不过是while其他答案所建议的循环包装。

回答by Acccumulation

The second line of your code creates a range object that is then used for the rest of the loop, and your code doesn't modify that range object. And even if you did change the step, that wouldn't change just the next element of the range, it would change the whole range object (that is, changing the step to 2 would make everyelement in the range 2 more than the previous). If you really want to, you can create a named object and modify it within the for loop, but that would be a rather messy thing to do.

代码的第二行创建一个范围对象,然后用于循环的其余部分,并且您的代码不会修改该范围对象。即使您确实更改了步长,也不会只更改范围的下一个元素,而是会更改整个范围对象(即,将步长更改为 2 会使范围 2 中的每个元素都比前一个多)。如果你真的想要,你可以创建一个命名对象并在 for 循环中修改它,但这将是一件相当麻烦的事情。

You can also use another index separate from the main for loop one. For instance:

您还可以使用与主 for 循环分开的另一个索引。例如:

actual_index = 1
for for_loop_index in range(1,100):
   if condition1:
        actual_index = actual_index + 1
   if condition2:
        actual_index = actual_index + 2
   if actual_index > 99:
        break

This would basically be a while loop, except with a hard limit on the number of iterations, which could be useful in some use cases.

这基本上是一个 while 循环,除非对迭代次数有严格限制,这在某些用例中可能很有用。

回答by bieboebap

import numpy as np
for i in np.arange(start,stop,stepwidth):
    # your stuff

回答by sainoba

You could add a skip check:

您可以添加跳过检查:

skip = 0
for i in range(1, 100):
    if skip != 0:
        skip -= 1
        continue
    if ...... :
        #do stuff
    else:
        skip = 1
        #do other stuff