[英]How to only keep nodes in networkx-graph with 2+ outgoing edges or 0 outgoing edges?
I have Directed Graph in networkx. 我在networkx中有Directed Graph。 I want to only keep those nodes which have two or more than two outgoing edges or no outgoing edge at all.
我想只保留那些具有两个或两个以上的传出边缘或根本没有传出边缘的节点。 How do I do this?
我该怎么做呢?
or 要么
How do I removes nodes which have exactly one outgoing edge in a networkx graph. 如何删除networkx图中只有一个传出边的节点。
You can find the nodes in graph G
with one outgoing edge using the out_degree
method: 您可以使用
out_degree
方法在图G
找到具有一个传出边的out_degree
:
outdeg = G.out_degree()
to_remove = [n for n in outdeg if outdeg[n] == 1]
Removing is then: 然后删除:
G.remove_nodes_from(to_remove)
If you prefer to create a new graph instead of modifying the existing graph in place, create a subgraph: 如果您希望创建新图形而不是修改现有图形,请创建子图形:
to_keep = [n for n in outdeg if outdeg[n] != 1]
G.subgraph(to_keep)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.