假设我有两个networkx图,G
和H
:
G=nx.Graph()
fromnodes=[0,1,1,1,1,1,2]
tonodes=[1,2,3,4,5,6,7]
for x,y in zip(fromnodes,tonodes):
G.add_edge(x,y)
H=nx.Graph()
fromnodes=range(2,8)
tonodes=range(8,14)
for x,y in zip(fromnodes,tonodes):
H.add_edge(x,y)
如何最佳方式将两个networkx图连接起来?
我希望保留节点名称(请注意,节点2到7是公共的)。当我使用nx.disjoint_union(G,H)
时,这并没有发生:
>>> G.nodes()
[0, 1, 2, 3, 4, 5, 6, 7]
>>> H.nodes()
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
>>> Un= nx.disjoint_union(G,H)
>>> Un.nodes()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
#
节点标签H
被更改了(不是我想要的)。我想将具有相同编号的节点连接到一起。
注意。这不是在NetworkX中合并两个加权图的重复问题。
U=nx.MultiGraph()
即可。 - Joelfor x,y in zip(fromnodes,tonodes): G.add_edge(x,y)
可以写成G.add_edges_from(zip(fromnodes,tonodes))
。 - Joel