networkx 的运行时错误:在运行神经气体脚本时在迭代期间字典发生了变化 [英] Runtime error with networkx : dictionary changed during iteration while running a neural gas script
问题描述
我正在尝试使用旧脚本运行神经气体网络,该脚本不适用于 networkx 2,因此我修改了一些内容.但是我收到错误:字典在迭代过程中改变了大小,我不知道如何解决这个问题,因为 networkx 不是我的专长.有什么帮助吗?
I am trying to run a neural gas network with an older script that doesn't work well with networkx 2 so I modified some things. However I am getting the error : Dictionary changed size during iteration and I don't get how to fix this because networkx is not my speciality. Any help?
现在导致问题的代码:
def prune_connections(self, a_max):
for u, v, attributes in self.network.edges(data=True):
if attributes['age'] > a_max:
self.network.remove_edge(u, v)
for u in self.network.nodes():
if self.network.degree(u) == 0:
self.network.remove_node(u)
以及我得到的错误:
in __iter__
for nbr, dd in nbrs.items():
RuntimeError: dictionary changed size during iteration
推荐答案
这里是遍历图的边缘:
对于u、v、self.network.edges(data=True)中的属性:
但是在该循环中,您可以修改边缘.所以 self.network.edges
(本质上是一个字典)在你迭代时会发生变化.这是python不允许的.
But within that loop you modify the edges. So self.network.edges
(which is fundamentally a dictionary) is changing while you're iterating. This isn't allowed by python.
对此的解决方案是预定义
A solution to this is to predefine
edgelist = list(self.network.edges(data=True))
然后做
对于 u、v、edgelist 中的属性:
这篇关于networkx 的运行时错误:在运行神经气体脚本时在迭代期间字典发生了变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!