473,394 Members | 1,742 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,394 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 11591
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 +...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.