2016-12-09 58 views
0

我需要將節點標記爲在我正在執行的遍歷中訪問過。所以我做G[node]['visited'] = True。然而,這個混亂了G.neighbors(node),給我'visited'作爲node的鄰居!處理這個問題的適當方法是什麼?Networkx將屬性作爲鄰居

實施例:

>>> import networkx as nx 
>>> G = nx.Graph() 
>>> G.add_edge(0,1) 
>>> G[0]['visited'] = True 
>>> G.neighbors(0) 
['visited', 1] 

回答

1

代替G[0]['visited'] = True使用G.node[0]['visited'] = True

你想要什麼的例子。您可以按照您設置的相同方式檢查屬性值。

>>> import networkx as nx 
>>> G = nx.Graph() 
>>> G.add_edge(0,1) 
>>> G.node[0]['visited'] = True 
>>> G.neighbors(0) 
[1] 
>>> G.node[0]['visited'] 
True 
+0

啊。看起來不錯!然後,如果我想要做邊緣屬性,我會做'G.edge [U] [V] [ '流'] = 50'? –

+0

是的。在這裏你可以使用G.edge [u] [v] ['flow'] = 50'或'G [u] [v] ['flow'] = 50'。 – edo

+0

但後者弄亂了鄰接列表,對吧? –