глобальные имена Python - PullRequest
       4

глобальные имена Python

0 голосов
/ 09 марта 2011

У меня проблемы с питоном. Я продолжаю получать ту же ошибку:

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 ]

2 голосов
/ 09 марта 2011

С чего бы это?узел не определен нигде в коде, который вы показали ... может быть, вы забыли импорт?

2 голосов
/ 09 марта 2011

В последней строке вы звоните node(label). Вы определили функцию node?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...