python-3.x 要打印树图的列表列表

but5z9lq  于 2022-11-26  发布在  Python
关注(0)|答案(2)|浏览(191)

我有一个由列表组成的树,类似于具有目录和文件递归列表的顶级目录。我想将其可视化为打印的树。

如何查看打印为树的列表列表?

数据

tree = [
    ['Main University'], 
    ['Main University', 'Academic Affairs'], 
    ['Main University', 'Academic Affairs', 'College of Health Sciences'], 
    ['Main University', 'Academic Affairs', 'College of Arts & Science'], 
    ['Main University', 'Academic Affairs', 'College of Arts & Science', 'Biology'], 
    ['Main University', 'Academic Affairs', 'College of Arts & Science', 'Chemistry/Physics'], 
    ['Main University', 'Academic Affairs', 'College of Arts & Science', 'Chemistry/Physics', 'Physics'], 
    ['Main University', 'Academic Affairs', 'College of Arts & Science', 'Biology', 'Biochemistry & Molecular Bio'], 
    ['Main University', 'Academic Affairs', 'College of Arts & Science', 'Biology', 'Earth Sciences'], 
    ['Main University', 'Academic Affairs', 'College of Arts & Science', 'Biology', 'Environmental Studies'], 
    ['Main University', 'Academic Affairs', 'College of Health Sciences', 'Social Work'], 
    ['Main University', 'Academic Affairs', 'College of Arts & Science', 'Chemistry/Physics', 'Chemistry'], 
    ['Main University', 'Academic Affairs', 'College of Health Sciences', 'Health Sciences'], 
    ['Main University', 'Academic Affairs', 'College of Health Sciences', 'Occupational Therapy']
]

所需输出(或类似;即,像¦--°--等字形无关紧要)

Main University                             
 °--Academic Affairs                        
     ¦--College of Arts & Science           
     ¦   ¦--Chemistry/Physics               
     ¦   ¦   ¦--Physics                     
     ¦   ¦   °--Chemistry                   
     ¦   °--Biology                         
     ¦       ¦--Biochemistry & Molecular Bio
     ¦       ¦--Earth Sciences              
     ¦       °--Environmental Studies       
     °--College of Health Sciences          
         ¦--Health Sciences                 
         ¦--Occupational Therapy            
         °--Social Work
sg3maiej

sg3maiej1#

如果数组不是太大,你可以先把它转换成树,然后打印出来

#!/bin/env python3
from collections import OrderedDict

tree = [
    ['Main University'],
    ['Main University', 'Academic Affairs'],
    ['Main University', 'Academic Affairs', 'College of Health Sciences'],
    ['Main University', 'Academic Affairs', 'College of Arts & Science'],
    ['Main University', 'Academic Affairs', 'College of Arts & Science', 'Biology'],
    ['Main University', 'Academic Affairs', 'College of Arts & Science', 'Chemistry/Physics'],
    ['Main University', 'Academic Affairs', 'College of Arts & Science', 'Chemistry/Physics', 'Physics'],
    ['Main University', 'Academic Affairs', 'College of Arts & Science', 'Biology', 'Biochemistry & Molecular Bio'],
    ['Main University', 'Academic Affairs', 'College of Arts & Science', 'Biology', 'Earth Sciences'],
    ['Main University', 'Academic Affairs', 'College of Arts & Science', 'Biology', 'Environmental Studies'],
    ['Main University', 'Academic Affairs', 'College of Health Sciences', 'Social Work'],
    ['Main University', 'Academic Affairs', 'College of Arts & Science', 'Chemistry/Physics', 'Chemistry'],
    ['Main University', 'Academic Affairs', 'College of Health Sciences', 'Health Sciences'],
    ['Main University', 'Academic Affairs', 'College of Health Sciences', 'Occupational Therapy']
]

class Tree:
    def __init__(self, data):
        self.data = data
        self.children = OrderedDict()

    def add_array(self, obj):
        if obj:
            if obj[0] not in self.children:
                self.children[obj[0]] = Tree(obj[0])
            self.children[obj[0]].add_array(obj[1:])

    def print_tree(self, l):
        s = " " * l * 4 + self.data + "\n"
        for x in self.children.values():
            s += x.print_tree(l+1)

        return s

    def __repr__(self):
        return self.print_tree(0)


root = Tree("root")
for x in tree:
    root.add_array(x)

print(root)

输出以下内容

root
    Main University
        Academic Affairs
            College of Health Sciences
                Social Work
                Health Sciences
                Occupational Therapy
            College of Arts & Science
                Biology
                    Biochemistry & Molecular Bio
                    Earth Sciences
                    Environmental Studies
                Chemistry/Physics
                    Physics
                    Chemistry
kmpatx3s

kmpatx3s2#

bigtree package可以很好地做到这一点:

from bigtree import list_to_tree, print_tree
path_list = ['/'.join(x) for x in tree]
root = list_to_tree(path_list)
print_tree(root)

收益率:

Main University
└── Academic Affairs
    ├── College of Health Sciences
    │   ├── Social Work
    │   ├── Health Sciences
    │   └── Occupational Therapy
    └── College of Arts & Science
        ├── Biology
        │   ├── Biochemistry & Molecular Bio
        │   ├── Earth Sciences
        │   └── Environmental Studies
        └── Chemistry
            └── Physics
                ├── Physics
                └── Chemistry

相关问题