首页 / PYTHON / python – 打印带缩进的树
python – 打印带缩进的树
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 打印带缩进的树,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3745字,纯文字阅读大概需要6分钟。
内容图文
![python – 打印带缩进的树](/upload/InfoBanner/zyjiaocheng/750/286f06fade04411c82b16f7bdcba3111.jpg)
我想打印出一个描述乐队的简单树.我首先创建一个名为“Band”的节点,然后让孩子成为“管乐器”,然后让孩子们成为“萨克斯管”和“小号”.然后我做了一个叫做“歌曲”的“管乐器”的兄弟姐妹等等.代码非常简单:
class Node:
value = ""
down = None
right = None
def write(p):
if p==None:
return
print(p.value)
if p.down!=None: #My idea is that if we go down the tree, we indent first
print(" ",end="")
write(p.down)
write(p.right) #If we don't go down, we simply write out the siblings
a=Node()
a.value="Band"
a.down=Node()
a.down.value="Wind instruments"
a.down.down=Node()
a.down.down.value="Saxophone"
a.down.down.right=Node()
a.down.down.right.value="Trumpet"
a.down.right=Node()
a.down.right.value="Song"
a.down.right.right=Node()
a.down.right.right.value="String instruments"
a.down.right.right.down=Node()
a.down.right.right.down.value="Guitar"
a.down.right.right.down.right=Node()
a.down.right.right.down.right.value="Bass"
write(a)
输出是:
Band
Wind instruments
Saxophone
Trumpet
Song
String instruments
Guitar
Bass
但我希望输出为:
Band
Wind instruments
Saxophone
Trumpet
Song
String instruments
Guitar
Bass
任何人都知道如何实现这一目标?
解决方法:
要根据递归级别创建缩进打印,诀窍是使用一个参数来保持递归时的级别:
# default with a level of 0, and an indent of 4 characters
def write(p, depth=0, indent=4):
if p==None:
return
# here we multiply the level by the number of indents
# and then you multiply that number with a space character
# which will magically show as that number of spaces.
print("{}{}".format(" "*(indent*depth), p.value))
if p.down!=None:
# then you do not need your print(…, end='') hack
# simply increase the depth
write(p.down, depth=depth+1, indent=indent)
# and for siblings, do not increase the depth
write(p.right, depth=depth, indent=indent)
我在这里使用的技巧是,默认情况下,级别为0,随着您的深入,您通过将参数增加1来增加深度.
然后,当你想打印出缩进时,你所要做的就是将缩进字符串与该值(以及缩进大小)相乘,然后你可以根据需要打印缩进:
>>> "A"*42
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
结果如下:
>>> write(a)
Band
Wind instruments
Saxophone
Trumpet
Song
String instruments
Guitar
Bass
如果你想让它更窄,因为你有很多递归:
>>> write(a, indent=1)
Band
Wind instruments
Saxophone
Trumpet
Song
String instruments
Guitar
Bass
作为奖励,我建议你将write()函数作为Node类的一种方法.如果你重命名它__str__:
class Node:
value = ""
down = None
right = None
# converts a node into a string
def as_str(self, depth=0, indent=4):
# building the current node's line, and add it to a list
ret = ["{}{}".format(" "*(indent*depth), self.value)]
if self.down:
# append down recursion first to the list
ret.append(self.down.as_str(depth=depth+1, indent=indent))
if self.right:
# then append right recursion to the list
ret.append(self.right.as_str(depth=depth, indent=indent))
# build the new string, joining each element of the list with a newline
return "\n".join(ret)
# a handler for printing the list nicely
def __str__(self):
return self.as_str()
def as_repr(self, depth=0, max_depth=2):
# building the current node's line, and add it to a list
ret = ["'{}'".format(self.value)]
if depth > max_depth:
ret.append("…")
else:
if self.down:
# append down recursion first to the list
ret.append(self.down.as_repr(depth=depth+1, max_depth=max_depth))
if self.right:
# then append right recursion to the list
ret.append(self.right.as_repr(depth=depth, max_depth=max_depth))
# build the new string, joining each element of the list with a newline
return "Node<{}>".format(",".join(ret))
# you might want to also make the repr() nicer
def __repr__(self):
return self.as_repr()
结果是:
>>> a
Node<'Band',Node<'Wind instruments',Node<'Saxophone',…>,Node<'Song',Node<'String instruments',Node<'Guitar',…>>>>>
>>> print(a)
Band
Wind instruments
Saxophone
Trumpet
Song
String instruments
Guitar
Bass
HTH
内容总结
以上是互联网集市为您收集整理的python – 打印带缩进的树全部内容,希望文章能够帮你解决python – 打印带缩进的树所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。