anzi5091104 2017-01-03 06:34 采纳率: 0%
浏览 1062

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 2017-01-03 10:40
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波