gpt4 book ai didi

python - 分层树 : (((A, B),(C,D)),E) 的视觉替代方案?

转载 作者:行者123 更新时间:2023-11-28 21:55:35 24 4
gpt4 key购买 nike

我有以下形式的分层树:

(((A,B),(C,D)),E)

有没有一种简单的方法来重新安排/绘制它(例如 Python)?

enter image description here

最佳答案

运行这一系列的替换:

  • ( -> <ul><li>
  • ) -> </ul></li>
  • , -> </li><li>

然后在浏览器中打开


或者如果是python对象,使用pprint:

>>> x = ((("A","B"),("C","D")),"E")
>>> from pprint import pprint
>>> pprint(x, width=1)
((('A',
'B'),
('C',
'D')),
'E')

或自定义 python 解决方案:

from itertools import izip

def first_then(first, then):
yield first
while True:
yield then

def tree_lines(x):
if type(x) is tuple:
if len(x) == 1:
# singular tuple
for p, l in izip(first_then('--', ' '), tree_lines(x[0])):
yield p + l
else:
first, rest, last = x[0], x[1:-1], x[-1]

# first entry
for p, l in izip(first_then('T-', '| '), tree_lines(first)):
yield p + l

# middle entries
for y in rest:
for p, l in izip(first_then('>-', '| '), tree_lines(y)):
yield p + l

# last entries
for p, l in izip(first_then('L-', ' '), tree_lines(last)):
yield p + l
else:
yield str(x)

x = ((('A','B'),('C','D')),'E')

for l in tree_lines(x):
print(l)

关于python - 分层树 : (((A, B),(C,D)),E) 的视觉替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22641553/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com