gpt4 book ai didi

python - 类型错误 : argument of type 'instance' is not iterable

转载 作者:太空宇宙 更新时间:2023-11-04 10:49:56 25 4
gpt4 key购买 nike

这是我的代码,它在行处中断:

 if (suc not in sFrontier) or (suc not in sExplored):

报错:TypeError: argument of type 'instance' is not iterable

 """
The pseudocode I'm following
initialize the frontier using the initial state of the problem
initialize the explored set to be empty
loop do
if the frontier is empty then return failure
choose a leaf node and remove it from the frontier
if the node contains a goal state then return the corresponding solution
add the node to the explored set
expand the chosen node, adding the resulting nodes to the frontier
only if not in the frontier or explored set
"""

sFrontier = util.Stack()
sFrontier.push(problem.getStartState())
sExplored = util.Stack()
lSuccessors = []

while not sFrontier.isEmpty():
leaf = sFrontier.pop()

if problem.isGoalState(leaf):
solution = []
while not sExplored.isEmpty():
solution[:0] = (sExplored.pop())[2]
return solution
sExplored.push(leaf)
lSuccessors = problem.getSuccessors(leaf)
for suc in lSuccessors:
if (suc not in sFrontier) or (suc not in sExplored):
sFrontier.push(suc)
return []

problem.getSuccessors 返回后继状态列表、它们需要的操作以及 1 的成本。

之后

lSuccessors = problem.getSuccessors(leaf)

l后继者打印

  [((5,4), 'South', 1), ((4,5), 'West', 1)]

及之后

  for suc in lSuccessors:

成功打印

  ((5,4), 'South', 1)

为什么会坏?是因为sFrontier和sExplored是栈,不能在栈中查找吗?

我需要一个 contain() 方法还是只使用一个列表?

感谢所有帮助:)

最佳答案

如果您的堆栈不支持包含测试,它确实会抛出错误。您需要添加一个 __contains__ method给他们以支持 in 测试。

in 测试可以通过其他方式在您的堆栈中找到项目,但不推荐使用它们,因为它们的效率不如 __contains__ 方法;查看in expression documentation .

关于python - 类型错误 : argument of type 'instance' is not iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14589833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com