漂亮在控制台窗口中以横向树格式输出 [英] Pretty Print output in a sideways tree format in console window
问题描述
我有一个使用Python创建的字典。
I have a dictionary such as this created using Python.
d = {'a': ['Adam', 'Book', 4], 'b': ['Bill', 'TV', 6, 'Jill', 'Sports', 1, 'Bill', 'Computer', 5], 'c': ['Bill', 'Sports', 3], 'd': ['Quin', 'Computer', 3, 'Adam', 'Computer', 3], 'e': ['Quin', 'TV', 2, 'Quin', 'Book', 5], 'f': ['Adam', 'Computer', 7]}
b $ b
我想以侧面的树形式而不是在控制台上打印出来。我试过漂亮的打印,但当字典变长,变得很难阅读。
I wanted to print this out in a sideways tree format rather on the console. I've tried pretty print but when the dictionary gets long, it becomes difficult to read.
例如,对于这个字典,它会返回:
For example, with this dictionary, it would return:
a -> Book -> Adam -> 4
b -> TV -> Bill -> 6
-> Sports -> Jill -> 1
-> Computer -> Bill -> 5
c -> Sports -> Bill -> 3
d -> Computer -> Quin -> 3
-> Adam -> 3
e -> TV -> Quin -> 2
Book -> Quin -> 5
f -> Computer -> Adam -> 7
本质上,漂亮的打印是由活动或列表中第二位置,然后按名称,然后按数字。
Essentially, the pretty print is organized by the Activity, or the item in second position in the list, then by name and then by the number.
上面的示例输出只是一个例子。我尝试使用漂亮的打印树,但无法找出如何把它变成侧面格式。
The sample output above is just an example. I tried working with Pretty print a tree but was unable to figure out how to turn that into a sideways format.
推荐答案
您可以看看 ETE工具包。函数 _asciiArt 会生成树的不错表示,即使内部节点标签
You can have a look at the code of the ETE toolkit. The function _asciiArt produces nice representations of trees even with internal node labels
from ete2 import Tree
t = Tree("(((A,B), C), D);")
print t
# /-A
# /---|
# /---| \-B
# | |
#----| \-C
# |
# \-D
这篇关于漂亮在控制台窗口中以横向树格式输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!