NetworkX:如何select 基于一组边缘索引的子图?
NetworkX: how to select subgraph based on a set of edge indexes?
对于:
G = nx.petersen_graph() # Example only, it could be any graph
E = G.edges()
while many_many_loops:
edge_set = my_function(...) # Returns a set, say { 3, 9, 4 }
T = G.edge_subgraph( ... ) # Contains only edges 3, 4, and 9.
考虑到 E
不可索引,我应该如何有效地为 edge_subgraph()
生成适当的参数?
(我能想到很多尴尬的方法,但我对 python 和 NetworkX 的了解仅足以让我认为 必须 是一个简单而优雅的方式,但还不足以知道它 是什么。)
是的,这很容易(当然 hind-sight 很明显):
E = list(G.edges())
T = G.edge_subgraph(E[e] for e in edge_set)
list()
函数将E转为列表,可以索引
对于:
G = nx.petersen_graph() # Example only, it could be any graph
E = G.edges()
while many_many_loops:
edge_set = my_function(...) # Returns a set, say { 3, 9, 4 }
T = G.edge_subgraph( ... ) # Contains only edges 3, 4, and 9.
考虑到 E
不可索引,我应该如何有效地为 edge_subgraph()
生成适当的参数?
(我能想到很多尴尬的方法,但我对 python 和 NetworkX 的了解仅足以让我认为 必须 是一个简单而优雅的方式,但还不足以知道它 是什么。)
是的,这很容易(当然 hind-sight 很明显):
E = list(G.edges())
T = G.edge_subgraph(E[e] for e in edge_set)
list()
函数将E转为列表,可以索引