使用 NetworkX 和 Python 对图的边进行分区
我想使用 Python 和 NetworkX 根据边缘属性对图 g 的边缘进行分区。在此代码片段中:
import networkx as nx
g = nx.Graph()
g.add_node(1, pos=[0, 0])
g.add_node(2, pos=[0, 2])
g.add_node(3, pos=[1, 1])
g.add_node(4, pos=[2, 1])
g.add_edge(1, 3, cat='a')
g.add_edge(4, 3, cat='a')
g.add_edge(1, 2, cat='b')
g.add_edge(4, 2, cat='b')
我想根据 cat
属性将图 g
划分为图 ga
和 gb
并保留节点属性pos
。请注意,节点 1
和 4
将同时属于 ga
和 gb
。有库支持此操作吗?
I would like to partition edges of a graph g
based on edge attributes, using Python and NetworkX. In this snippet:
import networkx as nx
g = nx.Graph()
g.add_node(1, pos=[0, 0])
g.add_node(2, pos=[0, 2])
g.add_node(3, pos=[1, 1])
g.add_node(4, pos=[2, 1])
g.add_edge(1, 3, cat='a')
g.add_edge(4, 3, cat='a')
g.add_edge(1, 2, cat='b')
g.add_edge(4, 2, cat='b')
I would like to partition graph g
into graphs ga
and gb
based on cat
attribute and retain node attribute pos
. Note that nodes 1
and 4
will belong to both ga
and gb
. Is there a library support for this operation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用列表理解和集合的组合:
you could use a combination of list comprehension and sets:
这似乎是一个简单的解决方案:
This seems to be a simple solution: