我遇到了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)
達到這條線,我無法找到與全局變量節點的任何問題,它應該工作。
謝謝!
您並未在代碼中的任何位置定義名爲'node'的變量或函數。 *它應該如何工作? – geoffspear
node_labels是一個集合。圖是一本字典。爲什麼你需要兩個節點? –
我假設,因爲它從network.txt中取出一個數字並將其列出: a = 1 b = 4 – Ranger