开发者

python global names

I'm having trouble with python. I keep getting the same error:

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)

up to this line, I can't find any problem with the global variable node, it should work.

Thanks!


Why should it? node is nowhere defined in the code you showed... maybe you forgot an import?


In the last line you are calling node(label). Have you defined the function node?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜