将 for 循环传递给 python 中的 any 函数意味着什么?
What does it mean when you pass a for loop into an any function in python?
我目前正在学习CS50's Introduction to Artificial Intelligence with Python。我遇到了一行对我来说没有意义的代码,我似乎无法在网上找到任何资源来向我解释这一点。
def contains_state(self,state):
return any(node.state == state for node in self.frontier)
这是Pythonclass中的一个方法。令我困惑的是我如何理解 node.state == state for node in self.frontier
?
我对 any() 的理解是它检查迭代的任何元素是否为 True 但上面的代码是如何工作的?
非常感谢您的热心帮助。
self.frontier
是一个可迭代对象。
node.state == state for node in self.frontier
遍历 self.frontier
并根据状态是否匹配创建一个新的 True 和 False 值列表。
any(...)
如果该列表中的任何一个包含 True,则返回 True。
大致相当于:
node_states = []
for node in self.frontier:
node_states.append(node.state == state)
return any(node_states)
这里发生了几件事:
- any() 函数
https://beginnersbook.com/2019/03/python-any-function/
Python any() function accepts iterable (list, tuple, dictionary etc.)
as an argument and return true if any of the element in iterable is
true, else it returns false. If iterable is empty then any() method
returns false.
- Python 可迭代
https://www.pythonlikeyoumeanit.com/Module2_EssentialsOfPython/Iterables.html
An iterable is any Python object capable of returning its members one
at a time, permitting it to be iterated over in a for-loop.
函数的 return 值: any(node.state == state for node in self.frontier)
Returns "true" if any "note.value" in any of the "nodes" in list "self.frontier" have same value as input parameter "state".
希望对您有所帮助...
any 中的代码是一个具有布尔值(True 或 False)的生成器对象。在执行 for 循环时,如果有 node.state == state
、contains_state returns True
.
与列表相比,使用生成器的优势在于,如果您找到状态与您要查找的状态相同的节点,则不必遍历每个元素。因此,在 some/most 情况下,它会 运行 更快。
如果它经过整个循环并且 none 个节点的状态等于传递给 contains_state
的状态,则函数 returns False
。您可以了解有关生成器的更多信息 here.
node.state == state for node in self.frontier
是一个带有 __next__
方法的生成器。当某些东西调用 __next__
时,生成器从 self.frontier
获取一个值,将其 state
变量与 state
和 returns 结果进行比较。当 self.frontier
引发 StopIteration
时,该异常将传递给调用者。
any()
是消费者。它调用 __next__
直到某事为真且 returns 为真。如果 __next__
加注 StopIteration
,它 returns False
.
例子
>>> state = 'foo'
>>> frontier = ['bar', 'bar', 'foo', 'bar']
>>> gen = (value == state for value in frontier)
>>> type(gen)
<class 'generator'>
>>> gen.__next__()
False
>>> gen.__next__()
False
>>> gen.__next__()
True
>>> gen.__next__()
False
>>> gen.__next__()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>>
>>> any(value == state for value in frontier)
True
我目前正在学习CS50's Introduction to Artificial Intelligence with Python。我遇到了一行对我来说没有意义的代码,我似乎无法在网上找到任何资源来向我解释这一点。
def contains_state(self,state):
return any(node.state == state for node in self.frontier)
这是Pythonclass中的一个方法。令我困惑的是我如何理解 node.state == state for node in self.frontier
?
我对 any() 的理解是它检查迭代的任何元素是否为 True 但上面的代码是如何工作的?
非常感谢您的热心帮助。
self.frontier
是一个可迭代对象。
node.state == state for node in self.frontier
遍历 self.frontier
并根据状态是否匹配创建一个新的 True 和 False 值列表。
any(...)
如果该列表中的任何一个包含 True,则返回 True。
大致相当于:
node_states = []
for node in self.frontier:
node_states.append(node.state == state)
return any(node_states)
这里发生了几件事:
- any() 函数
https://beginnersbook.com/2019/03/python-any-function/
Python any() function accepts iterable (list, tuple, dictionary etc.) as an argument and return true if any of the element in iterable is true, else it returns false. If iterable is empty then any() method returns false.
- Python 可迭代
https://www.pythonlikeyoumeanit.com/Module2_EssentialsOfPython/Iterables.html
An iterable is any Python object capable of returning its members one at a time, permitting it to be iterated over in a for-loop.
函数的 return 值:
any(node.state == state for node in self.frontier)
Returns "true" if any "note.value" in any of the "nodes" in list "self.frontier" have same value as input parameter "state".
希望对您有所帮助...
any 中的代码是一个具有布尔值(True 或 False)的生成器对象。在执行 for 循环时,如果有 node.state == state
、contains_state returns True
.
与列表相比,使用生成器的优势在于,如果您找到状态与您要查找的状态相同的节点,则不必遍历每个元素。因此,在 some/most 情况下,它会 运行 更快。
如果它经过整个循环并且 none 个节点的状态等于传递给 contains_state
的状态,则函数 returns False
。您可以了解有关生成器的更多信息 here.
node.state == state for node in self.frontier
是一个带有 __next__
方法的生成器。当某些东西调用 __next__
时,生成器从 self.frontier
获取一个值,将其 state
变量与 state
和 returns 结果进行比较。当 self.frontier
引发 StopIteration
时,该异常将传递给调用者。
any()
是消费者。它调用 __next__
直到某事为真且 returns 为真。如果 __next__
加注 StopIteration
,它 returns False
.
例子
>>> state = 'foo'
>>> frontier = ['bar', 'bar', 'foo', 'bar']
>>> gen = (value == state for value in frontier)
>>> type(gen)
<class 'generator'>
>>> gen.__next__()
False
>>> gen.__next__()
False
>>> gen.__next__()
True
>>> gen.__next__()
False
>>> gen.__next__()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>>
>>> any(value == state for value in frontier)
True