2016-09-10 19 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を参照してください理由です。

stackOb.push(problem.getStartState()) 
print(stackOb.list) 

や、スタックの先頭から要素を取得するためのpop()メソッドを実装して使用します:

class Stack: 
    # ... 

    def pop(self): 
     return self.list.pop() 

は代わりに、あなたはlist属性の内容を探るのいずれかに意味しました

peek()メソッドを使用して、スタックの先頭の要素を削除せずに返すだけの方法もあります。

class Stack: 
    # ... 

    def peek(self): 
     return self.list[-1] 
+0

ご返信ありがとうございます。それは助けた – user6622569

+0

@ user6622569大丈夫、確かに、あなたが答えを受け入れることができる場合は、ありがとう。 – alecxe

関連する問題