Python打印从列表pop返回,但在访问gives';非类型';不可下标错误
我正在以下代码中从堆栈插入和弹出:Python打印从列表pop返回,但在访问gives';非类型';不可下标错误,python,list,nonetype,Python,List,Nonetype,我正在以下代码中从堆栈插入和弹出: class Stack: "A container with a last-in-first-out (LIFO) queuing policy." def __init__(self): self.list = [] def push(self, item): "Push 'item' onto the stack" self.list.app
class Stack:
"A container with a last-in-first-out (LIFO) queuing policy."
def __init__(self):
self.list = []
def push(self, item):
"Push 'item' onto the stack"
self.list.append(item)
def pop(self):
"Pop the most recently pushed item from the stack"
return self.list.pop()
def isEmpty(self):
"Returns true if the stack is empty"
return len(self.list) == 0
open_stack=util.stack()
开始=[]
start.append(problem.getStartState())
打开堆栈。推送(启动)
而不是打开\u stack.isEmpty():
curr\u path=open\u stack.pop()
打印(类型(当前路径))
如果问题.isGoalState(当前路径[-1]):
返回当前路径
对于问题中的成功。GetSuccessivers(当前路径[-1]):
打开堆栈推送(当前路径追加(成功[0]))
返回错误
打印(类型(当前路径))返回:
class Stack:
"A container with a last-in-first-out (LIFO) queuing policy."
def __init__(self):
self.list = []
def push(self, item):
"Push 'item' onto the stack"
self.list.append(item)
def pop(self):
"Pop the most recently pushed item from the stack"
return self.list.pop()
def isEmpty(self):
"Returns true if the stack is empty"
return len(self.list) == 0
我得到的错误如下:
class Stack:
"A container with a last-in-first-out (LIFO) queuing policy."
def __init__(self):
self.list = []
def push(self, item):
"Push 'item' onto the stack"
self.list.append(item)
def pop(self):
"Pop the most recently pushed item from the stack"
return self.list.pop()
def isEmpty(self):
"Returns true if the stack is empty"
return len(self.list) == 0
文件“/home/ljagodz/uoft/search/search.py”,第101行,在depthFirstSearch中
如果问题.isGoalState(当前路径[-1]):
TypeError:“非类型”对象不可下标
我使用的堆栈类定义如下:
class Stack:
"A container with a last-in-first-out (LIFO) queuing policy."
def __init__(self):
self.list = []
def push(self, item):
"Push 'item' onto the stack"
self.list.append(item)
def pop(self):
"Pop the most recently pushed item from the stack"
return self.list.pop()
def isEmpty(self):
"Returns true if the stack is empty"
return len(self.list) == 0
我不明白为什么印刷品会这样,为什么我会出现这个错误。。
NoneType
错误似乎很常见,但我找不到任何类似于我在这里遇到的问题的解释,因为我可以告诉你,我没有像在其他引发此问题的Stackoverflow问题中那样,意外地将一些列表方法分配给变量。问题最有可能来自
...
for succ in problem.getSuccessors(curr_path[-1]):
open_stack.push(curr_path.append(succ[0]))
list
的append方法将返回None
对象,而不是对结果列表的引用。i、 e
print(curr_path.append(succ[0]))
将打印None
。因此,您将None
附加到堆栈中。尝试:
for succ in problem.getSuccessors(curr_path[-1]):
open_stack.push(curr_path + [succ[0]]) #use listcomp
也看到
p、 你也可以使用
编辑:小错误|