I have a list of lists that make up a tree, similar to a top level directory with a recursive listing of directories and files. I want to visualize this as a printed tree.
How can a see a list of lists printed as a tree?
Data
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']
]
Desired Output (or similar; i.e., glyphs like ¦--
, °--
, etc. don't matter)
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
CodePudding user response:
if the array is not too large you can convert it to a tree first then print it
#!/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)
prints out the following
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
CodePudding user response:
The bigtree package can do this nicely:
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)
Yields:
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