Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中使用堆栈会导致非类型错误_Python_Python 2.7_Stack - Fatal编程技术网

在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

我试图用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
    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好的,当然,看看你是否能接受答案,谢谢。