2011-03-09 70 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

相關問題