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 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据