Re: Making a tree out of a 2 column list

2007-04-19 Thread Jorgen Grahn
On Sat, 14 Apr 2007 11:37:11 -0300, Sebastian Bassi [EMAIL PROTECTED] wrote: I have a two column list like: 2,131 6,335 7,6 8,9 10,131 131,99 5,10 And I want to store it in a tree-like structure. CS side note: your columns describe a graph;

Re: Making a tree out of a 2 column list

2007-04-15 Thread Sebastian Bassi
On 4/15/07, Peter Otten [EMAIL PROTECTED] wrote: Depending on your input data you may need to add some cycle detection. For example, try it with tree_path(1, {1:[2], 2:[1]}, []) I guess this should make the program enter into a endless loop. But the data won't have such a redundancy, because

Re: Making a tree out of a 2 column list

2007-04-15 Thread bearophileHUGS
Sebastian Bassi: I guess this should make the program enter into a endless loop. But the data won't have such a redundancy, because it was taken from a philogenetic tree. But errors and bugs do happen, inside data too; so often it's better to be on safe side if the safe code is fast enough.

Re: Making a tree out of a 2 column list

2007-04-15 Thread Paul Rubin
[EMAIL PROTECTED] writes: I guess this should make the program enter into a endless loop. But the data won't have such a redundancy, because it was taken from a philogenetic tree. But errors and bugs do happen, inside data too; so often it's better to be on safe side if the safe code is

Re: Making a tree out of a 2 column list

2007-04-15 Thread Sebastian Bassi
On 15 Apr 2007 15:44:47 -0700, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: But errors and bugs do happen, inside data too; so often it's better to be on safe side if the safe code is fast enough. Yes, I agree since I've seen lot of errors in data. But this data comes from a taxonomy tree made by

Making a tree out of a 2 column list

2007-04-14 Thread Sebastian Bassi
I have a two column list like: 2,131 6,335 7,6 8,9 10,131 131,99 5,10 And I want to store it in a tree-like structure. So if I request 131, it should return all the child of 131, like 2, 10 and 5 (since 5 is child of 10). If I request 335, it should return: 6 and 7. If I request 9, it should

Re: Making a tree out of a 2 column list

2007-04-14 Thread [EMAIL PROTECTED]
On Apr 14, 9:37�am, Sebastian Bassi [EMAIL PROTECTED] wrote: I have a two column list like: 2,131 6,335 7,6 8,9 10,131 131,99 5,10 And I want to store it in a tree-like structure. So if I request 131, it should return all the child of 131, like 2, 10 and 5 (since 5 is child of 10).

Re: Making a tree out of a 2 column list

2007-04-14 Thread [EMAIL PROTECTED]
Hope this helps # list of pairs [child,parent] list=[[2,131],[6,335],[7,6],[8,9],[10,131],[131,99],[5,10]] # list with loop #list=[[2,131],[6,335],[7,6],[8,9],[10,131],[131,99],[5,10],[3,10], [131,3]] # put the pairs in a dictionary, for easy retrieval d={} for c,p in list: # must be

Re: Making a tree out of a 2 column list

2007-04-14 Thread bearophileHUGS
The solution I have found is quite similar to the [EMAIL PROTECTED] one (it uses a defaultdict, and it yields the result lazily), but it lacks the quite useful max_depth (it can be added): from collections import defaultdict def finder(el, stor): if el in stor: for v in stor[el]:

Re: Making a tree out of a 2 column list

2007-04-14 Thread Sebastian Bassi
On 14 Apr 2007 09:32:07 -0700, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: def tree_path(key,tree,indent): print '\t'*indent,key if tree.has_key(key): for m in tree[key]: tree_path(m,tree,indent+1) return Thank you. It worked!. I changed it a bit to return a

Re: Making a tree out of a 2 column list

2007-04-14 Thread Peter Otten
Sebastian Bassi wrote: On 14 Apr 2007 09:32:07 -0700, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: def tree_path(key,tree,indent):  print '\t'*indent,key  if tree.has_key(key):  for m in tree[key]:  tree_path(m,tree,indent+1)  return Thank you. It