在 Python 中打印树数据结构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20242479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing a Tree data structure in Python
提问by Kristof Pal
I was looking for a possible implementation of tree printing, which prints the tree in a user-friendly way, and not as an instance of object.
我正在寻找一种可能的树打印实现,它以用户友好的方式打印树,而不是作为对象的实例。
I came across this solution on the net:
我在网上遇到了这个解决方案:
source: http://cbio.ufs.ac.za/live_docs/nbn_tut/trees.html
来源:http: //cbio.ufs.ac.za/live_docs/nbn_tut/trees.html
class node(object):
def __init__(self, value, children = []):
self.value = value
self.children = children
def __repr__(self, level=0):
ret = "\t"*level+repr(self.value)+"\n"
for child in self.children:
ret += child.__repr__(level+1)
return ret
This code prints the tree in the following way:
此代码按以下方式打印树:
'grandmother'
'daughter'
'granddaughter'
'grandson'
'son'
'granddaughter'
'grandson'
Is it possible to have the same result but without changing the __repr__method, because I am using it for another purpose.
是否可以在不改变__repr__方法的情况下获得相同的结果,因为我将它用于其他目的。
EDIT:
编辑:
Solution without modifying __repr__and __str__
无需修改__repr__和解决方案__str__
def other_name(self, level=0):
print '\t' * level + repr(self.value)
for child in self.children:
child.other_name(level+1)
采纳答案by Martijn Pieters
Yes, move the __repr__code to __str__, then call str()on your tree or pass it to the printstatement. Remember to use __str__in the recursive calls too:
是的,将__repr__代码移至__str__,然后调用str()您的树或将其传递给print语句。记住__str__在递归调用中也使用:
class node(object):
def __init__(self, value, children = []):
self.value = value
self.children = children
def __str__(self, level=0):
ret = "\t"*level+repr(self.value)+"\n"
for child in self.children:
ret += child.__str__(level+1)
return ret
def __repr__(self):
return '<tree node representation>'
Demo:
演示:
>>> root = node('grandmother')
>>> root.children = [node('daughter'), node('son')]
>>> root.children[0].children = [node('granddaughter'), node('grandson')]
>>> root.children[1].children = [node('granddaughter'), node('grandson')]
>>> root
<tree node representation>
>>> str(root)
"'grandmother'\n\t'daughter'\n\t\t'granddaughter'\n\t\t'grandson'\n\t'son'\n\t\t'granddaughter'\n\t\t'grandson'\n"
>>> print root
'grandmother'
'daughter'
'granddaughter'
'grandson'
'son'
'granddaughter'
'grandson'
回答by Rambatino
Why don't you store it as a treelib objectand print it out similar to how we print the CHAID tree out herewith more relevant node descriptions related to your use case?
为什么不将它存储为一个treelib 对象并将其打印出来,类似于我们在此处打印 CHAID 树的方式以及与您的用例相关的更多相关节点描述?
([], {0: 809, 1: 500}, (sex, p=1.47145310169e-81, chi=365.886947811, groups=[['female'], ['male']]))
├── (['female'], {0: 127, 1: 339}, (embarked, p=9.17624191599e-07, chi=24.0936494474, groups=[['C', '<missing>'], ['Q', 'S']]))
│ ├── (['C', '<missing>'], {0: 11, 1: 104}, <Invalid Chaid Split>)
│ └── (['Q', 'S'], {0: 116, 1: 235}, <Invalid Chaid Split>)
└── (['male'], {0: 682, 1: 161}, (embarked, p=5.017855245e-05, chi=16.4413525404, groups=[['C'], ['Q', 'S']]))
├── (['C'], {0: 109, 1: 48}, <Invalid Chaid Split>)
└── (['Q', 'S'], {0: 573, 1: 113}, <Invalid Chaid Split>)

