2 anzi5091104 anzi5091104 于 2017.01.03 14:34 提问

python模块的问题,感觉模块没引入对。

下面这段python代码,运行提示
Traceback (most recent call last):
File "E:/project/python/可视化图/邻接表与邻接矩阵.py", line 43, in
gt.draw()
File "E:/project/python/可视化图/邻接表与邻接矩阵.py", line 36, in draw
self.G.node(str(node), str(node))
TypeError: 'dict' object is not callable
[1, 2, 5]
[2, 1, 5, 4, 3]
[3, 2, 4]
[4, 2, 5, 3]
[5, 1, 2, 4]

 import graphviz
import networkx as nx
from networkx.classes.graph import Graph

class GraphTable:
    def __init__(self, nodes, edges, is_dir = False):
        self.nodes = nodes
        self.edges = edges
        self.is_dir = is_dir
        self.graph = []
        for node in nodes:
            self.graph.append([node])
        for edge in edges:
            for n in self.graph:
                if n[0] == edge[0]:
                    n.append(edge[1])
                if not is_dir:
                    if n[0] == edge[1]:
                        n.append(edge[0])
        self.G = None

    def __str__(self):
        return '\n'.join([str(n) for n in self.graph])

    def draw(self):
        settings = dict(name='Graph', engine='circo', node_attr=dict(shape='circle'), format='png')
        self.G = nx.Digraph(**settings) if self.is_dir else Graph(**settings)
        for node in self.nodes:
            self.G.node(str(node), str(node))
        for edge in self.edges:
            self.G.edge(str(edge[0]), str(edge[1]))
        return self.G

gt = GraphTable([1, 2, 3, 4, 5], [(1, 2), (1, 5), (2, 5), (2, 4), (5, 4), (2, 3), (3, 4)])
print(gt)
gt.draw()

请问是什么问题?

1个回答

devmiao
devmiao   Ds   Rxr 2017.01.03 18:40
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!