2011-03-09 9 views
0

私はpythonに問題があります。pythonグローバル名

Traceback (most recent call last): 
    File "F:\test4", line 21, in <module> 
    graph = dict([(label, node(label))] for label in node_labels) 
    File "F:\test4", line 21, in <genexpr> 
    graph = dict([(label, node(label))] for label in node_labels) 
NameError: global name 'node' is not defined 
# open network.txt and populate nodes and close file 
network_file = open("network.txt", "r") 
lines = [line.strip() for line in network_file] 
network_file.close() 
print (len(lines)) 


# edges which will be populated with information in network.txt 
edges = []      # list of <label, label, distance> triples 
node_labels = set()    # set of node labels 
graph = {}      # dictionary of nodes keyed by labels 

for line in lines: 
    strNode, strNeighbor, strMetric = line.split()[:3] 
    intMetric = int(strMetric) 

    edges.append((strNode, strNeighbor, intMetric)) 
    node_labels.update([strNode, strNeighbor]) 

# create graph 
graph = dict([(label, node(label))] for label in node_labels) 

このラインまでは、私はグローバル変数ノードを持つ任意の問題は、それが動作するはず見つけることができません:私は同じエラーを得続けます。

ありがとうございます!

+2

あなたは、あなたが含むコードのどこにでも 'node'という名前の変数や関数を定義していません。 *どのようにすればうまくいくのですか? – geoffspear

+0

node_labelsはセットです。グラフは辞書です。なぜあなたは両方のノードを必要としますか? –

+0

私はそれがnetwork.txtから番号を取ってそれをリストしているので、私は仮定しました: a = 1 b = 4 – Ranger

答えて

2

なぜでしょうか? あなたが示したコードにノードがどこにも定義されていません...あなたがインポートを忘れた可能性がありますか?

+0

忘れてしまった – Ranger

2

最後の行では、node(label)と呼んでいます。関数nodeを定義しましたか?

関連する問題