Posts: 453
Threads: 16
Joined: Jun 2022
Would that not look like this?
names = {
"John": {
"David": {
"Grunt": None
},
"Jade": None
}
}
Sig:
>>> import this
The UNIX philosophy: "Do one thing, and do it well."
"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse
"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Posts: 453
Threads: 16
Joined: Jun 2022
I don't think that the speed difference between the two will be even noticeably. For me, I find the dictionary more "human readable". As for building that from a DB; there's always a way.
Sig:
>>> import this
The UNIX philosophy: "Do one thing, and do it well."
"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse
"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Posts: 4,790
Threads: 76
Joined: Jan 2018
You could do it like this
>>> from collections import defaultdict
>>> builder = defaultdict(dict)
>>> pairs = [('John', 'Null'), ('David', 'John'), ('Grunt','David'), ('Jade', 'John')]
>>> for node, parent in pairs:
... builder[parent][node] = builder[node]
...
>>> print(builder['Null'])
{'John': {'David': {'Grunt': {}}, 'Jade': {}}}
>>>
SpongeB0B and
rob101 like this post
« We can solve any problem by introducing an extra level of indirection »
Posts: 4,790
Threads: 76
Joined: Jan 2018
(Dec-11-2023, 10:01 AM)SpongeB0B Wrote: I would like to print a hierarchy
What have you tried? Post your code!
« We can solve any problem by introducing an extra level of indirection »
Posts: 4,790
Threads: 76
Joined: Jan 2018
Dec-11-2023, 09:19 PM
(This post was last modified: Dec-11-2023, 09:19 PM by Gribouillis.)
(Dec-11-2023, 10:01 AM)SpongeB0B Wrote: The ultimate goal is to great an html output ~like this
I think you should generate unformatted html code, then process it with
bs4.BeautifulSoup.prettify()
Custom
formatters can be created to alter the result of
prettify()
« We can solve any problem by introducing an extra level of indirection »