1
我對NetworkX documentation的閱讀表明這應該起作用,但似乎沒有?將NetworkX MultiDiGraph轉換爲字典或從字典中轉換
考慮:
import networkx as nx
g = nx.MultiDiGraph()
g.add_nodes_from([0, 1])
g.add_edge(0,1)
g.add_edge(0,1)
g.edges() # returns [(0, 1), (0, 1)]
d = nx.to_dict_of_dicts(g) # returns {0: {1: {0: {}, 1: {}}}, 1: {}}
g2 = nx.from_dict_of_dicts(d, multigraph_input=True)
# or, equivalently?, g2 = MultiDiGraph(d)
g2.edges() # only returns [(0,1)]
我在這裏做一個簡單的錯誤,或者這是一個錯誤?
對於我的應用程序,我發現了一個更好的選擇,它使用networkx.readwrite.json_graph
進行序列化,但我認爲我會在這裏留下問題以防其他人有用。
非常好!非常感謝:)我會想'multigraph_input = True'會照顧到這一點,錯過了'create_using' arg。 – Matthew