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;
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
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.
[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
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
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
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).
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
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]:
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
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
11 matches
Mail list logo