1

我正在编写一个算法来表示回归树,使用 pydot 模块(Graphviz 的 Dot 语言的接口)。在算法中,边​​和节点的列表被创建,然后它们被表示——这工作正常。

但在某些特定情况下,我需要删除一些边缘和节点,这就是我卡住的地方。以下是部分代码:

import pydot
graph = pydot.Dot(graph_type='graph')

link4 = pydot.Edge(node10, node21, label=etiquetas[3])
link5 = pydot.Edge(node11, node22, label=etiquetas[4])
lista_links = [link4, link5]

# if some conditions are verified, then:
lista_links.remove(link5)

for link in lista_links:
graph.add_edge(link)
graph.write_png('teste.png')

我原以为这段代码可以正常工作,但我得到一个错误,说:

AttributeError: 'NoneType' object has no attribute 'get_top_graph_type'

我唯一的想法是,不是在某些特定情况下删除节点和边缘,而是在我定义所有特定情况后更改代码并仅添加节点和边缘。但这将是更多的工作......(代码比我向您展示的要大得多,并且我有几个需要考虑的具体情况)。

我很好奇为什么python会有这样的行为......有人可以向我解释一下,或者给我任何关于如何改变这种行为的想法吗?

在此先感谢,卡拉

4

1 回答 1

1

At the surface, it seems that the problem is in Edges or Nodes without parent graph. So, the overall solution would be: do not allow nodes and edges hang around, always attach them to graph,and then remove from graph as needed.

于 2011-07-15T16:01:39.087 回答