[英]Why doesn't the value of my boolean change in a while-loop?
My algorithms book has an implementation of a linked list on Python with different methods.我的算法书用不同的方法在 Python 上实现了一个链表。 This is the
remove
method, but I don't fully understand the while
loop.这是
remove
方法,但我不完全理解while
循环。
This implementation relies on a Node
class that was previously define, the class has methods such as get.data()
, get.next()
, set_next()
, etc. The list itself is a class called UnorderedList
.此实现依赖于
Node
类先前定义的类有方法,如get.data()
get.next()
set_next()
等名单本身就是一种叫做类UnorderedList
。 self.head
refers to the first item in the list. self.head
指的是列表中的第一项。
The idea of this remove
method is to traverse the list until we find the item that we want to remove.这个
remove
方法的想法是遍历列表,直到找到我们想要删除的项目。 We create a current
and a previous
to keep track of the current item we are looking at and the one that came before.我们创建一个
current
和一个previous
来跟踪我们正在查看的当前项目和之前的项目。
My question has to do with the while loop.我的问题与 while 循环有关。 We set
found
to False
with the intention of changing it when we find the item we are looking for.我们将
found
设置为False
是为了在找到我们要查找的项目时更改它。 However, wouldn't the while loop change the value of found
to True
when we say not found
?但是,当我们说
not found
时,while 循环不会将found
的值更改为True
吗?
I know it's a simple question, but I am having a hard time understanding why and how this while loop works since in my head not
usually makes the boolean in its scope the opposite of what it is.我知道这是一个简单的问题,但我很难理解这个 while 循环为什么以及如何工作,因为在我的脑海中通常
not
使布尔值的范围与它的相反。 Could anyone explain this to me?有人可以向我解释一下吗?
btw I don't need any explanation or improvement on the current code, it's an example from the book and it all makes sense to me, I just want to understand that part.顺便说一句,我不需要对当前代码进行任何解释或改进,这是书中的一个示例,对我来说都很有意义,我只是想了解那部分。
def remove(self, item):
current = self.head
previous = None
found = False
while not found:
if current.get_data() == item:
found = True
else:
previous = current
current = current.get_next()
if previous == None:
self.head = current.get_next()
else:
previous.set_next(current.get_next())
So it seems like I was missing part of the point of what the while-loop was trying to do.所以似乎我错过了 while 循环试图做的事情的一部分。 Indeed, saying
not found
means that the expression evaluates to True
, but in this case that doesn't stop the loop.事实上,说
not found
意味着表达式的计算结果为True
,但在这种情况下,这不会停止循环。 It does exactly what we want it to, it continues to run the loop.它完全按照我们的意愿去做,它继续运行循环。 Once we find the item we are looking for, we change the value of
found
to be True
.一旦我们找到了我们正在寻找的项目,我们将
found
的值更改为True
。 At that point, when we evaluate while not found
it will yield False
, which will get us out of the loop.在这一点上,当我们评估
while not found
,它将产生False
,这将使我们退出循环。
not found
is used as a conditional in the while loop, not as a statement that changes the value. not found
在 while 循环中用作条件,而不是用作更改值的语句。 You can read while not found
as while found == False
您可以
while not found
while found == False
阅读为while found == False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.