473,378 Members | 1,134 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Dictionary to tree format (hopefully simple)

Hi!
I have seemingly simple problem, which no doubt someone out there has
already solved, but I'm stumped. Imagine I have a dictionary like
below, in which the keys are parent nodes and the values are a list of
children nodes.

dict = { 0: [1, 2], 1: [3], 2: [6], 3: [4,7], 4: [5,8], 8: [9] }

Is there an obvious way to produce a nested tree format for this
dictionary, like this:

[ 0 [ 1 [ 3 [ 4 [ 5 , 8 [ 9 ] ] , 7 ] ] , 2 [ 6 ] ]

Thanks for any help,

Adam
Aug 5 '08 #1
3 11586
Adam Powell schrieb:
Hi!
I have seemingly simple problem, which no doubt someone out there has
already solved, but I'm stumped. Imagine I have a dictionary like
below, in which the keys are parent nodes and the values are a list of
children nodes.

dict = { 0: [1, 2], 1: [3], 2: [6], 3: [4,7], 4: [5,8], 8: [9] }

Is there an obvious way to produce a nested tree format for this
dictionary, like this:

[ 0 [ 1 [ 3 [ 4 [ 5 , 8 [ 9 ] ] , 7 ] ] , 2 [ 6 ] ]

Thanks for any help,
d = { 0: [1, 2], 1: [3], 2: [6], 3: [4,7], 4: [5,8], 8: [9] }

nodelists = dict((node ,[node, []]) for node in d.keys())

for node, children in d.iteritems():
for child in children:
nodelists[node][1].extend(nodelists.setdefault(child, [child]))

print nodelists[0]
Two remarks:

- don't use "dict" as name for a dictionary - look at the above code
*why* not...

- the code assumes that 0 is the root. if that wouldn't be the case,
you need to search for the root node. I've got an idea - you to?

Diez
Aug 5 '08 #2
Thanks very much for this, very concise!
Aug 6 '08 #3
Diez B. Roggisch wrote:
Adam Powell schrieb:
>Hi!
I have seemingly simple problem, which no doubt someone out there has
already solved, but I'm stumped. Imagine I have a dictionary like
below, in which the keys are parent nodes and the values are a list of
children nodes.

dict = { 0: [1, 2], 1: [3], 2: [6], 3: [4,7], 4: [5,8], 8: [9] }

Is there an obvious way to produce a nested tree format for this
dictionary, like this:

[ 0 [ 1 [ 3 [ 4 [ 5 , 8 [ 9 ] ] , 7 ] ] , 2 [ 6 ] ]

Thanks for any help,

d = { 0: [1, 2], 1: [3], 2: [6], 3: [4,7], 4: [5,8], 8: [9] }

nodelists = dict((node ,[node, []]) for node in d.keys())

for node, children in d.iteritems():
for child in children:
nodelists[node][1].extend(nodelists.setdefault(child, [child]))

print nodelists[0]
Two remarks:

- don't use "dict" as name for a dictionary - look at the above code
*why* not...

- the code assumes that 0 is the root. if that wouldn't be the case,
you need to search for the root node. I've got an idea - you to?

Diez
Not quite. That gets you

[0, [1, [3, [4, [5, 8, [9]], 7]], 2, [6]]]

which probably isn't what you want. Note that the children of 0 are

1, [3, [4, [5, 8, [9]], 7]],
2,
[6]

which probably isn't what was desired.
You probably want

[0, [1, [3, [4, [5], [8, [9]]], [7]]], [2, [6]]]

so that each list is [node, [children]].

The original poster wanted

[ 0 [ 1 [ 3 [ 4 [ 5 , 8 [ 9 ] ] , 7 ] ] , 2 [ 6 ] ]

but that's not a meaningful Python list expression.

Try this recursive form:

d = { 0: [1, 2], 1: [3], 2: [6], 3: [4,7], 4: [5,8], 8: [9] }

def getsubtree(d, node) :
if d.has_key(node) :
return([node] + [getsubtree(d,child) for child in d[node]])
else :
return([node])

getsubtree(d,min(d.keys())) # smallest node is assumed to be the root

John Nagle
Aug 7 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Reckless | last post by:
I've got a file with this in it: The data I'd like extracted is within the quotes: Some string data I can read the file out and extract (using string positions) the data I'd like but it...
4
by: peterbe | last post by:
I want to match a word against a string such that 'peter' is found in "peter bengtsson" or " hey peter," or but in "thepeter bengtsson" or "hey peterbe," because the word has to stand on its own....
9
by: Jon Thackray | last post by:
I'm trying to use XPath to enable me to number items within sections in a paper. I have so far tried about ten things, all of which it seemed to me should work, and none of which do. Essentially...
1
by: graham bates | last post by:
Hi There, In desperation I was wondering whether anyone could cast their eye over a small problem I having. I am attempting to create a user control which basically consists of a DropDown list....
6
by: alexrussell101 | last post by:
For anyone who can't be bothered to read my code and examples, scroll to the bottom, the question's there. Thanks. I'm using php and regular expressions to convert bbcode style things to html....
5
markmcgookin
by: markmcgookin | last post by:
Hi Folks, Happy new year to all! I have two questions here, which I hope will be relatively simple you you guys to answer! 1) Is it possible to stick a line or two of code in a form somewhere...
1
by: bobmct | last post by:
Again: struggling to convert old-style HTML to html using css. I am trying to create a rectangular box divided into approx three vertical sections. The top section contains an image I would like...
1
by: Andrew | last post by:
I'm learning PHP so this might be a simple question to more expeienced developers hopefully. I have a findrecords.php page which has the following code which creates a new record in a database...
1
by: techie929 | last post by:
I have to display nodes in bst in the below given format: I have insesrted the nodes properly and able to display it using preorder. if the nodes are F,D,E...the output should come as: F +...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.