我遇到了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)
直到这一行,我找不到全局变量节点的任何问题,它应该可以工作。
谢谢!
答案 0 :(得分:2)
在最后一行,您正在呼叫node(label)
。您是否定义了函数node
?
答案 1 :(得分:2)
为什么要这样? 节点在你展示的代码中没有定义...也许你忘记了导入?