简体   繁体   English

如何在Python 3中为每个for循环迭代将step变量加倍?

[英]How do I double my step variable for each for-loop iteration in Python 3?

I'm new to Python, and I'm playing around with recursive functions just for practice. 我是Python的新手,我只是为了练习而使用递归函数。

I made the following algorithm which takes a number as x and halves it until it is equal to 1. n is the number of times x has been halved. 我制作了以下算法,将数字取为x并将其减半直到等于1。nx减半的次数。

def binary(x, n = 0):
    print(n,":",x)
    x = x // 2
    n += 1
    if x > 0:
       binary(x, n)
       return x
    return x

I'm trying to make a loop that will call binary() with multiple values for x. 我正在尝试制作一个循环,该循环将调用带有多个x值的binary()。 I want my step to be doubled each iteration. 我希望我的步骤在每次迭代中加倍。 I have it working with a while loop like the one below. 我让它像下面的循环一样工作。

i = 1
while i < 1000000000:
    print("when x is", i, ":")
    binary(i)
    i += i

For some reason though, I can't seem to achieve the same thing with a For loop. 但是由于某种原因,我似乎无法通过For循环实现相同的目的。 Here's what I have now. 这就是我现在所拥有的。

for i in range(1,1000):
    print("when x is", i, ":")
    binary(i)
    i += i

In the code above, i += i does not seem to effect the i in my header. 在上面的代码中, i += i似乎不影响标题中的i I know that range() takes a third parameter called step, but I've tried this: 我知道range()需要一个名为step的第三个参数,但是我已经尝试过了:

for i in range(1,1000, i += i):
    # statements

This gives me a name error, and says "i is not defined". 这给我一个名称错误,并说“我未定义”。 Most of my experience with programming is in JavaScript and C#. 我大部分的编程经验是使用JavaScript和C#。 In both of those languages I wouldn't of had any trouble doing this. 使用这两种语言,我都不会遇到任何麻烦。

How would I get this to work in a For loop using Python 3? 如何使用Python 3在For循环中使用它?

The third parameter of range is indeed step. range的第三个参数的确是步进。 But you should use it with a computed value like: 但是您应该将其与以下计算值一起使用:

for i in range(1,1000,2):
    #statements

The reason why your i += i didn't worked is because, under the hood, the for-loop is executing something similar to i = next(...) at the end of an iteration, overiding your last increment. 您的i += i无法工作的原因是,在后台,for循环在迭代结束时执行类似于i = next(...)的操作,从而覆盖了您的最后一个增量。

Edit 编辑

You can achieve the desired effect using a generator, but it kinda kills the "trying to avoid while-loops" thing: 您可以使用生成器来达到所需的效果,但是它会杀死“试图避免while循环”的事情:

def myrange(start, stop):
    i = start
    while i < stop:
        yield i
        i += i

for i in myrange(1, 1000):
    print(i)

Anyway, while-loops are perfectly valid constructs and I'd personnally go with one in this case. 无论如何,while循环是完全有效的构造,在这种情况下,我会亲自使用一个。 Do not forget that the for-loop has a rather different semantic in python than in both languages you're used to. 不要忘记,for循环在python中的语义与您习惯的两种语言完全不同。 So trying to use a for-loop because you are able to do so with javascript seems like a bad idea if all what you need is a while-loop. 因此,如果您需要的只是while循环,则尝试使用for循环是因为您可以使用javascript这样做似乎是一个坏主意。

range can step by a fixed amount, but not a variable amount. range可以逐步增加一个固定的数量,但不能变化。 Use a while-loop to increment i by i : 使用while-loopi增加i

i += i

You could replace the while-loop with an iterator, such as: 您可以用迭代器替换while-loop ,例如:

import itertools as IT
for i in (2**i for i in IT.count()):
    if i >= 1000000000: break
    print("when x is", i, ":")
    binary(i)

but I don't think this has any advantage over a simple while-loop . 但是我认为这比简单的while-loop没有任何优势。

If all you're doing is doubling i , then why not just raise it to the power? 如果您要做的只是将i加倍,那么为什么不将其提高呢?

for p in range(int(1000000000**0.5)):
    print(binary(2**p)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM