2016-09-10 32 views
-2

我试图推动一个项目在Python的堆栈。下面是试图推动该项目的代码:在python中使用堆栈给出了NoneType错误

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) 

在搜索类print语句给出类型为无

任何建议如何解决这个问题? 感谢

回答

0

你要打印push()方法调用的结果,但不返回任何的方法 - 这就是为什么你看到None打印。

相反,你的意思是要么探索list属性的内容:

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] 
+0

谢谢您的回复。它帮助 – user6622569

+0

@ user6622569好的,当然,看看你能否接受答案,谢谢。 – alecxe