如何在python中显示类似于msdos树命令的树?

问题描述 投票:2回答:2

考虑一下这个mcve:

import sys


dct = {
    -1: [0, 60000],
    0: [100, 20],
    100: [30],
    30: [400, 500],
    60000: [70, 80]
}


def ptree(parent, tree, indent=''):
    print(parent)
    if parent not in tree:
        return

    for child in tree[parent][:-1]:
        print(indent + '|' + '-' * 4, end='')
        ptree(child, tree, indent + '|' + ' ' * 4)
    child = tree[parent][-1]
    print(indent + '`' + '-' * 4, end='')
    ptree(child, tree, indent + '  ' * 4)


ptree(-1, dct)

这个小代码有两个问题:

  • 项目-1在此上下文中被视为“隐形根项目”,因此我不想打印它
  • 这个命令的输出非常难看,并且也是坏对齐的,我想得到一个类似于tree显示的输出

为了解决第一个问题,我考虑过将这些丑陋的条件/黑客引入代码:

def ptree(parent, tree, indent=''):
    if parent != -1:
        print(parent)
    if parent not in tree:
        return

    for child in tree[parent][:-1]:
        if parent != -1:
            print(indent + '|' + '-' * 4, end='')
            ptree(child, tree, indent + '|' + ' ' * 4)
        else:
            ptree(child, tree, indent)
    child = tree[parent][-1]
    if parent != -1:
        print(indent + '`' + '-' * 4, end='')
        ptree(child, tree, indent + '  ' * 4)
    else:
        ptree(child, tree, indent)

对于第二个要点,我不太清楚如何实现它,但msdos树命令显示的输出非常好,我希望我的命令以完全相同的方式显示树,这是一个命令将会是什么命令的示例看起来像:

enter image description here

问题:您如何调整上述代码,以便正确解决上述2个问题?

python tree
2个回答
2
投票

通过反复试验,我不知何故最终找到了一个解决方案,它似乎完全吐出了原始树所产生的东西:

def ptree(start, tree, indent_width=4):

    def _ptree(start, parent, tree, grandpa=None, indent=""):
        if parent != start:
            if grandpa is None:  # Ask grandpa kids!
                print(parent, end="")
            else:
                print(parent)
        if parent not in tree:
            return
        for child in tree[parent][:-1]:
            print(indent + "├" + "─" * indent_width, end="")
            _ptree(start, child, tree, parent, indent + "│" + " " * 4)
        child = tree[parent][-1]
        print(indent + "└" + "─" * indent_width, end="")
        _ptree(start, child, tree, parent, indent + " " * 5)  # 4 -> 5

    parent = start
    _ptree(start, parent, tree)


dct = {
    -1: [0, 60000],
    0: [100, 20, 10],
    100: [30],
    30: [400, 500],
    60000: [70, 80, 600],
    500: [495, 496, 497]
}

除了使用正确的连接器之外,检查爷爷并增加最后一次ptree-call的4到5的缩进是关键。


ptree(-1, dct)
# Out
├────0
│    ├────100
│    │    └────30
│    │         ├────400
│    │         └────500
│    │              ├────495
│    │              ├────496
│    │              └────497
│    ├────20
│    └────10
└────60000
     ├────70
     ├────80
     └────600

1
投票

第一个问题很简单:检查父值;如果它是-1,请不要打印它。

压痕量是根据节点值的打印图像而不是恒定的4个空格而移位的问题。 math包有log10ceil方法来完成这项工作。

import sys
import math


dct = {
    -1: [0, 60000],
    0: [100, 20, 7],
    100: [30],
    30: [400, 500],
    60000: [70, 80],
    7: [9, 11, 13],
}


def ptree(parent, tree, indent=''):

    if parent != -1:
        print(parent)

    if parent not in tree:
        return

    shift = math.ceil(math.log10(parent)) \
            if parent >= 10 else 1
    indent += ' ' * shift

    for child in tree[parent][:-1]:
        print(indent + '|' + '-' * 4, end='')
        ptree(child, tree, indent + '|' + ' ' * 4)

    child = tree[parent][-1]
    print(indent + '`' + '-' * 4, end='')
    ptree(child, tree, indent + ' ' * 4)


ptree(-1, dct)

输出:

 |----0
 |     |----100
 |     |      `----30
 |     |            |----400
 |     |            `----500
 |     |----20
 |     `----7
 |          |----9
 |          |----11
 |          `----13
 `----60000
          |----70
          `----80
© www.soinside.com 2019 - 2024. All rights reserved.