问题描述
我正在寻找一种可能的树打印实现,它以用户友好的方式打印树,而不是作为对象的实例.
我在网上发现了这个解决方案:
来源:http://cbio.ufs.ac.za/live_docs/nbn_tut/trees.html
类节点(对象):def __init__(self, value, children = []):self.value = 价值self.children = 孩子def __repr__(self, level=0):ret = "\t"*level+repr(self.value)+"\n"对于 self.children 中的孩子:ret += child.__repr__(level+1)返回 ret
此代码以下列方式打印树:
'祖母''女儿''孙女''孙子''儿子''孙女''孙子'
是否可以在不更改 __repr__
方法的情况下获得相同的结果,因为我将其用于其他目的.
不修改__repr__
和__str__
def other_name(self, level=0):打印 '\t' * level + repr(self.value)对于 self.children 中的孩子:child.other_name(level+1)
是的,将 __repr__
代码移动到 __str__
,然后调用 str()
code> 或将其传递给 print
语句.记住在递归调用中也要使用 __str__
:
类节点(对象):def __init__(self, value, children = []):self.value = 价值self.children = 孩子def __str__(self, level=0):ret = "\t"*level+repr(self.value)+"\n"对于 self.children 中的孩子:ret += child.__str__(level+1)返回 retdef __repr__(self):return ''
演示:
>>>root = node('祖母')>>>root.children = [node('daughter'), node('son')]>>>root.children[0].children = [node('孙女'), node('孙子')]>>>root.children[1].children = [node('孙女'), node('孙子')]>>>根<树节点表示>>>>字符串(根)"'祖母'\n\t'女儿'\n\t\t'孙女'\n\t\t'孙子'\n\t'儿子'\n\t\t'孙女'\n\t\t'孙子'\n">>>打印根'祖母''女儿''孙女''孙子''儿子''孙女''孙子'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
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.
EDIT:
Solution without modifying __repr__
and __str__
def other_name(self, level=0):
print '\t' * level + repr(self.value)
for child in self.children:
child.other_name(level+1)
Yes, move the __repr__
code to __str__
, then call str()
on your tree or pass it to the print
statement. Remember to use __str__
in the recursive calls too:
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'
这篇关于在 Python 中打印树数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!