[英]Python boolean variable not updating within a loop
I have two pieces of python code that I expect to give me the same result but that is not happening.我有两段 python 代码,我希望它们能给我相同的结果,但这并没有发生。 I wrote the following routine for a fill type question on leetcode and got the wrong answer我为 leetcode 上的填充类型问题编写了以下例程并得到了错误的答案
def processIsland(self, grid: List[List[int]], proc: List[List[int]], i: int, j: int) -> bool:
n = len(grid)
m = len(grid[0])
proc[i][j] = 1
trav = [0, 1, 0, -1, 0]
val = True
for k in range(4):
ip = i+trav[k]
jp = j+trav[k+1]
if 0 <= ip < n and 0 <= jp < m:
if grid[ip][jp] == 0 and proc[ip][jp] == 0:
val = val and self.processIsland(grid, proc, ip, jp)
if i == 0 or i == n-1 or j == 0 or j == m-1:
return False
return val
However when I changed it store the boolean values returned by the recursive call to the function in separate variables and take the AND at the end, my solution was accepted.但是,当我更改它时,将递归调用函数返回的布尔值存储在单独的变量中并在最后采用 AND,我的解决方案被接受了。
def processIsland(self, grid: List[List[int]], proc: List[List[int]], i: int, j: int) -> bool:
n = len(grid)
m = len(grid[0])
proc[i][j] = 1
trav = [0, 1, 0, -1, 0]
val = [True, True, True, True]
for k in range(4):
ip = i+trav[k]
jp = j+trav[k+1]
if 0 <= ip < n and 0 <= jp < m:
if grid[ip][jp] == 0 and proc[ip][jp] == 0:
val[k] = self.processIsland(grid, proc, ip, jp)
if i == 0 or i == n-1 or j == 0 or j == m-1:
return False
val = val[0] and val[1] and val[2] and val[3]
return val
I am quite perplexed as I thought that they should have identical behavior.我很困惑,因为我认为他们应该有相同的行为。 I am new to Python so I don't know if I'm missing anything obvious.我是 Python 新手,所以我不知道我是否遗漏了任何明显的东西。 Any help is appreciated.任何帮助表示赞赏。
I am not entirely sure what that function is supposed to calculate, but note that and
is lazy, ie the 'right' expression is only evaluated if the 'left' expression is not already false.我不完全确定该函数应该计算什么,但请注意and
是惰性的,即仅在“左”表达式尚未为假时才评估“右”表达式。 In your case this means that在您的情况下,这意味着
val = val and self.processIsland(grid, proc, ip, jp)
will only execute the recursive call if val
is not already False
from an earlier iteration.仅当val
在早期迭代中还不是False
时才会执行递归调用。 And since those recursive calls have side-effect (eg they modify grid
and proc
) that might be a problem.而且由于这些递归调用具有副作用(例如它们修改grid
和proc
),这可能是一个问题。
The second version, on the other hand, always executes all four recursive calls.另一方面,第二个版本总是执行所有四个递归调用。 If you want to make that a bit prettier, you can just return all(val)
though, instead of combining them with and
in the previous line.如果你想让它更漂亮一点,你可以只return all(val)
,而不是将它们与上一行中的and
结合起来。
Understand the logical operators.理解逻辑运算符。
For logical AND : If the first operator is True then only it checks next operator Ex.对于逻辑AND :如果第一个运算符为 True,则仅检查下一个运算符 Ex。
def hello():
print("Hello")
def world():
print("World")
a = True
b = False
c = a and hello()
c = b and world()
Output: Hello
Explanation:
a is true therefore hello() function is executed.
b is false therefore world() function is not executed.
Therefore your code does not makes recursive calls when val is False in first case.因此,当val在第一种情况下为False时,您的代码不会进行递归调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.