利用NetworkX中某些边属性有效地提取子图

2024-09-29 05:20:07 发布

您现在位置:Python中文网/ 问答频道 /正文

通过指定节点列表,可以很容易地从NetworkX图中提取子图,但是我找不到一种有效的方法来执行子图的边提取。例如,提取子图包含的边的权重超过用户定义的阈值。在

目前我的做法是:

## extracts all edges satisfy the weight threshold (my_network is directed):
eligible_edges = [(from_node,to_node,edge_attributes) for from_node,to_node,edge_attributes in my_network.edges(data=True) if edge_attributes['weight'] > threshold]
new_network = NetworkX.DiGraph()
new_network.add_edges_from(eligible_edges)

有更好的方法吗?在

谢谢你的回答。在


Tags: to方法fromnetworkxnodenewthreshold节点
1条回答
网友
1楼 · 发布于 2024-09-29 05:20:07

这看起来是最好的解决办法。在

您可以使用graph.edges_iter()而不是graph.edges()来节省内存

>>> G = nx.DiGraph(((source, target, attr) for source, target, attr in my_network.edges_iter(data=True) if attr['weight'] > threshold))

相关问题 更多 >