在python中使用堆栈会导致非类型错误
我试图用Python将一个项目推送到堆栈上。以下是尝试推送项目的代码:在python中使用堆栈会导致非类型错误,python,python-2.7,stack,Python,Python 2.7,Stack,我试图用Python将一个项目推送到堆栈上。以下是尝试推送项目的代码: class Search def generalGraphSearch(problem,fringe): closed=set() #If no nodes if problem.isGoalState(problem.getStartState()): return problem.getStartState() #Create object of Stack class s
class Search
def generalGraphSearch(problem,fringe):
closed=set()
#If no nodes
if problem.isGoalState(problem.getStartState()):
return problem.getStartState()
#Create object of Stack class
stackOb = util.Stack()
"""Push the starting node into the stack. The parameter is the state"""
stackOb.push(problem.getStartState())
print stackOb.push(problem.getStartState())
The stack implementation is as below :
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)
Search类中的print语句将type指定为none
有什么建议可以克服这个问题吗?
谢谢您正在尝试打印push方法调用的结果,但是该方法没有返回任何内容-这就是为什么您没有看到打印结果的原因 相反,您希望浏览列表属性的内容:
stackOb.push(problem.getStartState())
print(stackOb.list)
或者,实现并使用pop方法从堆栈顶部获取元素:
class Stack:
# ...
def pop(self):
return self.list.pop()
您还可以使用peek方法,该方法只需从堆栈返回顶部元素,而无需删除它:
class Stack:
# ...
def peek(self):
return self.list[-1]
谢谢你的回复。信息技术helped@user6622569好的,当然,看看你是否能接受答案,谢谢。