Python 字典 : Needed a better output

标签 python dictionary trie

伙计们,我写了一段代码来给出输出。现在,这是一个尝试。现在我想以一种审美的方式展示。有人帮帮我。我的表示应该是这样的 http://en.wikipedia.org/wiki/File:Trie_example.svg

但我想要的是如何将这个巨大的怪物输出转换为像这样的整洁输出 [(1,2), (1,3), (1,4), (3,4)] ????

defaultdict(<type 'int'>, {'A': 1, 'W': 12})
defaultdict(<type 'int'>, {'A': 2, 'X': 25})
defaultdict(<type 'int'>, {'A': 3})
defaultdict(<type 'int'>, {'A': 4})
defaultdict(<type 'int'>, {'S': 5})
defaultdict(<type 'int'>, {'S': 6})
defaultdict(<type 'int'>, {'S': 7})
defaultdict(<type 'int'>, {'D': 8})
defaultdict(<type 'int'>, {'D': 9})
defaultdict(<type 'int'>, {'D': 10})
defaultdict(<type 'int'>, {'D': 11})
defaultdict(<type 'int'>, {})
defaultdict(<type 'int'>, {'R': 16, 'E': 13, 'F': 19})
defaultdict(<type 'int'>, {'E': 14})
defaultdict(<type 'int'>, {'E': 15})
defaultdict(<type 'int'>, {})
defaultdict(<type 'int'>, {'R': 17})
defaultdict(<type 'int'>, {'T': 18})
defaultdict(<type 'int'>, {})
defaultdict(<type 'int'>, {'F': 20})
defaultdict(<type 'int'>, {'F': 21})
defaultdict(<type 'int'>, {'D': 22})
defaultdict(<type 'int'>, {'D': 23})
defaultdict(<type 'int'>, {'D': 24})
defaultdict(<type 'int'>, {})
defaultdict(<type 'int'>, {'C': 26})
defaultdict(<type 'int'>, {'C': 27})
defaultdict(<type 'int'>, {'V': 28})
defaultdict(<type 'int'>, {'S': 29})
defaultdict(<type 'int'>, {})

最佳答案

你应该试试 Pydot !这个包允许你创建一些图形线:

import pydot 

edges = [(1,2), (1,3), (1,4), (3,4)] 
g = pydot.graph_from_edges(edges) 
g.write_jpeg('graph_from_edges_dot.jpg', prog='dot')

The result

安装它:

pip install pydot

(如果愿意,您也可以使用 easy_installPyPM)

pydot 需要 pyparsing为了加载 DOT 文件和 graphviz渲染图形。

如果你想有一个png图片(例如),你可以替换行

g.write_jpeg('graph_from_edges_dot.jpg', prog='dot')

通过

g.write('graph_from_edges_dot.png', prog='dot', format='png')

g.write_png('graph_from_edges_dot.png', prog='dot')

完整文档可在此处获得: PyDot documentation

关于Python 字典 : Needed a better output,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5441555/

相关文章:

string - Trie 插入或读取操作中的逻辑问题

python - 如何在 Django 中添加字段名称作为变量?

java - 在命令行或Rest客户端中执行脚本

python - 使用推导式将 Python 字典数组转换为 Python 字典

Java:在资源中存储一张大 map

java - 尝试最长前缀匹配

java - 可以在数组上使用迭代器吗?

python - "Cannot update a query once a slice has been taken"。最佳实践?

python - matlibplot - 网络字节 y 轴到人类可读

ios - 在 UITableView 中选择具有过滤结果的行时,如何获取实际的数组索引值? swift