2016-05-15 18 views
-1

私はDijkstra's Algorithmのクラスを書くことができます。私はダイクストラクラスを編集することはできませんよけど:NoneTypeオブジェクトは反復可能ではありませんエラー

class Dijkstra(): 
# initialize with a string containing the root and a 
# weighted edge list 
def __init__(self, in_string): 
    self.root, self.nnodes, self.adj_list = self.convert_to_adj_list(in_string) 
    self.nodes = [Node(i) for i in range(self.nnodes)] 
    self.nodes[self.root].key = 0 
    self.heap = MinHeap(self.nodes) 
# the input is expected to be a string 
# consisting of the number of nodes 
# and a root followed by 
# vertex pairs with a non-negative weight 
def convert_to_adj_list(self, in_string): 
    nnodes, root, edges = in_string.split(';') 
    root = int(root) 
    nnodes = int(nnodes) 
    adj_list = {} 
    edges = [ map(int,wedge.split()) for wedge in edges.split(',')] 
    for u,v,w in edges: 
     (adj_list.setdefault(u,[])).append((v,w)) 
    for u in range(nnodes): 
     adj_list.setdefault(u,[]) 

これは私の問題です:

string = '3; 0; 1 2 8, 2 0 5, 1 0 8, 2 1 3' 
print(Dijkstra(string)) 
Traceback (most recent call last): 
    File "<pyshell#321>", line 1, in <module> 
    print(Dijkstra(string)) 
    File "C:\Users\TheDude\Downloads\dijkstra.py", line 71, in __init__ 
    self.root, self.nnodes, self.adj_list = self.convert_to_adj_list(in_string) 
TypeError: 'NoneType' object is not iterable 

は私がappendの戻り値を割り当てていますか?そして、編集なしでどうすればいいのですか? class Djikstra() 読書用タンク。割当作業に

self.root, self.nnodes, self.adj_list = self.convert_to_adj_list(in_string) 

ためには

+0

'あなたがreturn文でそれを提供しなかったので、' NONE'を返しconvert_to_adj_list'。 – miradulo

+0

'convert_to_adj_list'からの戻り値が必要です。' convert_to_adj_list'定義の最後に 'return adj_list'を追加してください。 –

+0

戻り値を設定しないと、関数はNoneを返します。 私はDijkstraクラスを使用する必要がありますので、これを修正するには先生に連絡する必要があります。 – TheDude

答えて

2

convert_to_adj_listは、これら3つの変数に展開される3つの値のタプルを返さなければなりません。しかし、あなたのメソッドは何も返しません(暗黙的にNoneを返します)。あなたのconvert_to_adj_list方法は、それが動作するはずです、このようなものに変更します。

def convert_to_adj_list(self, in_string): 
    ... your code ... 
    return root, nnodes, adj_list 
+0

ありがとう、あなたtobias! – TheDude

関連する問題