我执行以下 python 命令,试图从图 G
中删除所有边。
def method(G):
edges = G.edges()
G.remove_edges_from(edges)
但它会产生以下错误:
RuntimeError: dictionary changed size during iteration.
看起来命令遍历边,但是当我删除边时,它会在迭代期间修改迭代器,这会导致错误。
我该如何解决这个问题?
最佳答案
您可能需要查看方法 create_empty_copy
,它将没有边的节点复制到一个新图中。文档字面意思是:
Return a copy of the graph G with all of the edges removed.
如果你想保持原图不变,你可以做类似的事情
edges = list(G.edges)
确保在请求移除边缘之前复制边缘。
关于python - 从 networkx 中的图形中删除所有边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52251556/