简体   繁体   English

删除networkx中的孤立顶点

[英]removing isolated vertices in networkx

The documentation says that isolated vertices in graph can be obtained using networkx.isolates( G ). 文档说明图中的孤立顶点可以使用networkx.isolates( G )获得。 It adds that the isolated vertices can be removed from a graph G using the code G .remove_nodes_from(nx.isolates( G )). 它补充说,可以使用代码G .remove_nodes_from(nx.isolates( G ))从图G中移除孤立的顶点。

https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.algorithms.isolate.isolates.html https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.algorithms.isolate.isolates.html

文档截图(上面的url)

But I get the run time error "dictionary changed size during iteration" when I run the code. 但是当我运行代码时,我得到运行时错误“字典在迭代期间改变了大小”。

Error report:- 错误报告:-
>>> G.remove_nodes_from(nx.isolates(G)) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/iiitdm/anaconda2/lib/python2.7/site-packages/networkx/classes/graph.py", line 617, in remove_nodes_from for n in nodes: File "/home/iiitdm/anaconda2/lib/python2.7/site-packages/networkx/algorithms/isolate.py", line 94, in <genexpr> return (n for n, d in G.degree() if d == 0) File "/home/iiitdm/anaconda2/lib/python2.7/site-packages/networkx/classes/reportviews.py", line 443, in __iter__ for n in self._nodes: RuntimeError: dictionary changed size during iteration

It is understandable and was expected because (I think) the generator object created using the function isolates() changes with G and hence changing graph G while it is being 'iterated' should give a similar error. 这是可以理解的并且是预期的,因为(我认为)使用函数isolates()创建的生成器对象随G更改,因此在“迭代”时更改图G应该给出类似的错误。 Then that line in the documentation must be wrong, isn't it? 那么文档中的那一行肯定是错的,不是吗? Am I completely off the mark? 我完全没有了吗? I am pretty new to python. 我对python很新。

By the way, the object returned by networkx.isolates() is a generator object. 顺便说一句,networkx.isolates()返回的对象是一个生成器对象。

Thank you 谢谢

I think you are right, submit a documentation patch? 我认为你是对的,提交文件补丁?

also you can cast the generator to a list to get around this: 您也可以将生成器强制转换为列表以解决此问题:

G.remove_nodes_from(list(nx.isolates(G)))

But why does your work-around work? 但为什么你的解决方案工作? I don't understand it; 我不明白; the situation has not changed! 情况没有改变!

I would have to look at their code, but my hunch is the lazyness of a generator is working against it based on the Exception message. 我将不得不看看他们的代码,但我的预感是生成器的懒惰正在根据异常消息对其进行处理。

Casting to list, the collection is created before it is fed as an argument, so there is no side effects on the object as it is iterated. 转换为列表,集合在作为参数提供之前创建,因此在迭代时对对象没有副作用。

As noted from @Dyz's answer, the documentation is correct, you are using Nx 2.0. 从@ Dyz的回答中可以看出,文档是正确的,您使用的是Nx 2.0。

https://networkx.github.io/documentation/networkx-2.0/reference/algorithms/generated/networkx.algorithms.isolate.isolates.html https://networkx.github.io/documentation/networkx-2.0/reference/algorithms/generated/networkx.algorithms.isolate.isolates.html

Does this collection creation before fed as argument behaviour hold for any kind of cast (say to dict or set) ? 这个集合创建在作为参数行为被提供之前是否适用于任何类型的演员(比如说dict或set)? -

Well not quite (a set will work) dict won't because it wants a pair of items. 好吧不完全(一套会工作)dict不会因为它想要一对物品。 list and set when called like a function (and dict but again it needs pairs (a list of tuples will work)) calls __iter__ 当像函数一样被调用时listset (和dict但它又需要对(元组列表将起作用))调用__iter__

Generators have __iter__ which makes them iterables (+ a lot of other objects). 生成器有__iter__ ,这使得它们可迭代 (+许多其他对象)。 Generators are really nice to have to address various usecases, for example when you have a large collection of items, and need to loop through them multiple times, it can save you on runtime complexity. 生成器非常适合处理各种用例,例如,当您拥有大量项目并需要多次循环时,它可以节省运行时的复杂性。

However, there is nuances such as what you ran into, where you have to understand some of the internals for proper use. 然而,有一些细微差别,比如你遇到了什么,你必须了解一些正确使用的内部。

You are looking at the docs for 1.X while using 2.X. 在使用2.X时,您正在查看1.X的文档。 Unfortunately the search engine ranking is higher for the 1.X documentation. 不幸的是,1.X文档的搜索引擎排名更高。

In networkx 2.X isolates is a generator object. 在networkx 2.X隔离区是一个生成器对象。

The current stable docs will add list() to the example code. 当前稳定的文档会将list()添加到示例代码中。

https://networkx.github.io/documentation/stable/reference/algorithms/generated/networkx.algorithms.isolate.isolates.html https://networkx.github.io/documentation/stable/reference/algorithms/generated/networkx.algorithms.isolate.isolates.html

In [107]: nx.isolates(G)
Out[107]: <generator object isolates.<locals>.<genexpr> at 0x7fa499cd8e60>

The migration guide may help out if you are expecting 1.X behavior too. 如果您还期望1.X行为,迁移指南可能会有所帮助。

https://networkx.github.io/documentation/stable/release/migration_guide_from_1.x_to_2.0.html https://networkx.github.io/documentation/stable/release/migration_guide_from_1.x_to_2.0.html

You confuse networkx-2.0 (where isolates returns an iterator) and the documentation for networkx-1.10 (where isolates returns a list). 您混淆networkx-2.0isolates返回迭代器)和networkx-1.10文档( isolates返回列表)。 Your code would work perfectly fine in networkx-1.10 . 您的代码在networkx-1.10 Applying list to isolates in 2.0 reduces the situation to what existed in 1.10. list应用于2.0中的isolates将情况简化为1.10中存在的情况。

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

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