Making a tree out of a 2 column list

Sebastian Bassi sbassi at clubdelarazon.org
Sat Apr 14 17:56:02 EDT 2007


On 14 Apr 2007 09:32:07 -0700, mensanator at aol.com <mensanator at aol.com> 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 list with the results:

def tree_path(key,tree,hijos):
	hijos.append(key)
	if tree.has_key(key):
		for m in tree[key]:
			tree_path(m,tree,hijos)
	return hijos

Then I call it like this:

MyList=tree_path(9608,tree,[])

Best,
SB.



More information about the Python-list mailing list