简体   繁体   English

Python嵌套for循环变量'overwrite'

[英]Python nested for loop variable 'overwrite'

Sorry for the noob question, but someone pls explain me the inner workings of this nested for loop. 对于noob问题很抱歉,但有人请解释一下这个嵌套for循环的内部工作原理。

v,w = 2,4
for v in range(v):
   for w in range(w):
        print('v=',v,'w=',w)

If I run it like this, the output will be as follows: 如果我像这样运行它,输出将如下:

v= 0 w= 0
v= 0 w= 1
v= 0 w= 2
v= 0 w= 3
v= 1 w= 0
v= 1 w= 1
v= 1 w= 2

I figured it stops 'prematurely' (the last output v= 1 w= 3 is missing) cause the last value assigned to the 'w' variable was 3 before the last loop ran. 我认为它“过早地”停止(最后一个输出v = 1 w = 3缺失)导致在最后一个循环运行之前分配给'w'变量的最后一个值是3。

If I run it like this, it works, but doesn't look Pythonic to say the least. 如果我像这样运行它,它可以工作,但至少可以说Pythonic。

v,w = 2,4
for v in range(v):
    for w in range(w):
        print('v=',v,'w=',w)
    w = 4

Could some please explain how this problem is best addressed? 有人可以解释一下如何最好地解决这个问题?

I figured it stops 'prematurely' (the last output v= 1 w= 3 is missing) cause the last value assigned to the 'w' variable was 3 before the last loop ran 我认为它'过早停止'(最后一个输出v = 1 w = 3缺失)导致分配给'w'变量的最后一个值在最后一个循环运行之前为3

Correct. 正确。

Could some please explain how this problem is best addressed? 有人可以解释一下如何最好地解决这个问题?

Don't re-use the same local variable name for two different meanings: 不要为两个不同的含义重复使用相同的局部变量名称:

n_v, n_w = 2, 4
for v in range(n_v):
   for w in range(n_w):
        print('v=', v, 'w=', w)

The most interesting thing to notice here in my opinion is that range(w) is not updated as long as you stay in the same for v in range(v) iteration. 在我看来,最值得注意的是, 只要for v in range(v)迭代中保持相同的range(w)就不会更新。 And that is why you get: 这就是你得到的原因:

v = 0,  w = 0
v = 0,  w = 1
v = 0,  w = 2
v = 0,  w = 3

But , on the second iteration, when your v becomes 1 , range(w) gets redefined using the new w value which is now 3 . 但是 ,在第二次迭代中,当你的v变为1 ,使用新的w值(现在为3 range(w) 重新定义 range(w) And that is why you then get: 这就是为什么你得到:

v = 1,  w = 0
v = 1,  w = 1
v = 1,  w = 2

Addressing the problem as for example @wim says is simple; 解决问题,例如@wim说的很简单; you just have to use different variables: 你只需要使用不同的变量:

for i in range(v):
    for j in range(w):
        # do your thing

A side-effect of the above is that the following does execute 5 times even though it is a horrible thing to write: 上述的副作用是,即使编写一个可怕的东西,以下也会执行5次:

v = 5
for v in range(v):
    # do your thing

Hi Peter see the answer from wim he is correct. 嗨,彼得从wim看到答案他是对的。

Rewrite the code like below:- 重写如下代码: -

v,w = 2,4
  for v1 in range(v):
     for w1 in range(w):
         print('v=',v1,'w=',w1)

This behavior is known as variable leaking in for loops and is due to the fact that Python does not create a new variable in for loops. 这种行为称为for循环中的变量泄漏 ,这是因为Python不会在for循环中创建新变量。

At the end of for w in range(w): , w has value 3 so when you run the loop again it will run for range(3) and for range(4) with the original value of w as you might have expected. for w in range(w):for w in range(w):结束时, w值为3,因此当您再次运行循环时,它将运行范围(3)和范围(4),其原始值为w如您所料。

To avoid this, use a different variable for in the for loop, as suggested in the other answers. 为避免这种情况,请在for循环中使用其他变量,如其他答案中所示。

The only case when the for loop variable is not exposed to the surrounding function is in list comprehensions (and only in Python 3, see PEP- Generator Expressions ), so for instance for循环变量未暴露给周围函数的唯一情况是列表推导(并且仅在Python 3中,参见PEP- Generator表达式 ),例如

>>> for v in range(v):
...    print([('v=',v,'w=',w) for w in range(w)])
...    print(w)
...
[('v=', 0, 'w=', 0), ('v=', 0, 'w=', 1), ('v=', 0, 'w=', 2), ('v=', 0, 'w=', 3)]
4
[('v=', 1, 'w=', 0), ('v=', 1, 'w=', 1), ('v=', 1, 'w=', 2), ('v=', 1, 'w=', 3)]
4

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

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