[英]How do you find nodes with no outgoing edges in networkx?
如果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.