简体   繁体   English

你如何在networkx中找到没有传出边的节点?

[英]How do you find nodes with no outgoing edges in networkx?

I am trying to find nodes with no outgoing edges in a digraph in networkx.我试图在 networkx 的有向图中找到没有传出边的节点。

Is there a way to do this?有没有办法做到这一点? I found isolates but that finds edges without incoming or outgoing edges, and I don't want that.我找到了隔离,但找到了没有传入或传出边缘的边缘,我不想要那样。

如果G是你的DiGraph ,你可以通过

(node for node, out_degree in G.out_degree_iter() if out_degree == 0)

I had a problem with out_degree_iter() as it returns an error.我遇到了out_degree_iter()问题,因为它返回错误。 So I searched for other solutions on NetworkX documentation and found this is working.所以我在 NetworkX 文档上搜索了其他解决方案,发现这是有效的。 ( G is a DiGraph ) G是有DiGraph

[node for node in G.nodes if G.out_degree(node) == 0]

Note, in case of finding incoming degree or edges.请注意,在查找传入度数或边缘的情况下。 You can just simply change from out_degree to in_degree您可以简单地从out_degree更改为in_degree

Ref: networkx.DiGraph.out_degree参考: networkx.DiGraph.out_degree

"methods that returned an iterator have been removed " “返回迭代器的方法已被删除

in NetworkX 2.x, so @fuglede's answer needs a minor update:在 NetworkX 2.x 中,所以@fuglede 的回答需要一个小的更新:

(node for node, out_degree in G.out_degree() if out_degree == 0)

The view/reporting API provided by .out_degree in 2.x provides an OutDegreeView over (node, out_degree) pairs, making this approach slightly simpler than @Fony Lew's. 2.x 中.out_degree提供的视图/报告 API 在(node, out_degree)对上提供了 OutDegreeView,使这种方法比 @Fony Lew 的方法稍微简单一些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM