Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在networkx中有有向图。我只想保留那些有两个或两个以上出边或根本没有出边的节点。我该怎么做呢?
或者
如何删除 networkx 图中恰好有一个传出边的节点。
G您可以使用以下方法在图中找到具有一条出边的节点out_degree:
G
out_degree
outdeg = G.out_degree() to_remove = [n for n in outdeg if outdeg[n] == 1]
删除是:
G.remove_nodes_from(to_remove)
如果您更喜欢创建新图而不是就地修改现有图,请创建一个子图:
to_keep = [n for n in outdeg if outdeg[n] != 1] G.subgraph(to_keep)