读取 MultiDiGraph 作为 dict-of-dicts 忽略 multigraph_input
Reading MultiDiGraph as dict-of-dicts ignores multigraph_input
在 NetworkX 2.5 中,我似乎无法使用 dict-of-dicts 读取多图中。这是一个尝试往返某些数据的示例:
G = nx.MultiDiGraph()
G.add_edge(2, 3, weight=5)
G.add_edge(2, 3, weight=4) # Create 2 edges
dict_of_dicts = nx.to_dict_of_dicts(G)
G2 = nx.MultiDiGraph(dict_of_dicts, multigraph_input=True)
print("Original", G.edges(data=True))
print("Round tripped", G2.edges(data=True))
print(G2.number_of_edges(), "edge(s) in round tripped graph")
在往返图 G2 中只给出一条边:
Original [(2, 3, {'weight': 5}), (2, 3, {'weight': 4})]
Round tripped [(2, 3, {0: {'weight': 5}, 1: {'weight': 4}})]
1 edge(s) in round tripped graph
那么在 MultiDiGraph 中作为 dict-of-dicts 读取的格式是什么?
对我来说,使用带有 create_using
和 multigraph_input
参数的 nx.from_dict_of_dicts
有效:
import networkx as nx
G = nx.MultiDiGraph()
G.add_edge(2, 3, weight=5)
G.add_edge(2, 3, weight=4) # Create 2 edges
dict_of_dicts = nx.to_dict_of_dicts(G)
G2 = nx.from_dict_of_dicts(dict_of_dicts, create_using=nx.MultiDiGraph(), multigraph_input=True)
在 NetworkX 2.5 中,我似乎无法使用 dict-of-dicts 读取多图中。这是一个尝试往返某些数据的示例:
G = nx.MultiDiGraph()
G.add_edge(2, 3, weight=5)
G.add_edge(2, 3, weight=4) # Create 2 edges
dict_of_dicts = nx.to_dict_of_dicts(G)
G2 = nx.MultiDiGraph(dict_of_dicts, multigraph_input=True)
print("Original", G.edges(data=True))
print("Round tripped", G2.edges(data=True))
print(G2.number_of_edges(), "edge(s) in round tripped graph")
在往返图 G2 中只给出一条边:
Original [(2, 3, {'weight': 5}), (2, 3, {'weight': 4})]
Round tripped [(2, 3, {0: {'weight': 5}, 1: {'weight': 4}})]
1 edge(s) in round tripped graph
那么在 MultiDiGraph 中作为 dict-of-dicts 读取的格式是什么?
对我来说,使用带有 create_using
和 multigraph_input
参数的 nx.from_dict_of_dicts
有效:
import networkx as nx
G = nx.MultiDiGraph()
G.add_edge(2, 3, weight=5)
G.add_edge(2, 3, weight=4) # Create 2 edges
dict_of_dicts = nx.to_dict_of_dicts(G)
G2 = nx.from_dict_of_dicts(dict_of_dicts, create_using=nx.MultiDiGraph(), multigraph_input=True)