trees, iterations and adding leaves

vertigo spam at spam.pl
Sun Dec 31 12:25:19 EST 2006


Hello

I have trees like this:

>>> from nltk_lite.parse.tree import Tree
>>> tree6 = Tree('main', ['sub1', 'sub2'])
>>> tree6
('main': 'sub1' 'sub2')

I use nltk package - but it should not matter here.
I could change it's lafes (add node) like this:
>>> tree6[0] = Tree('newsub',[])
>>> tree6
('main': ('newsub': ) 'sub2')

But my tree has many levels and it's imposibble to address it like:
tree6[0][1][0][1][1][1][0]..........

So i wanted to 'travel thru my tree' to last node which should be changed:
>>> tree6 = Tree('main', ['sub1', 'sub2'])
>>> subtree = tree6[0]
>>> subtree
'sub1'
>>> subtree = Tree('newsub',[])
>>> subtree
('newsub': )
>>> tree6
('main': 'sub1' 'sub2')

The problem is that subtree is some kind of a new variable (not pointer)
so changing it i will not alter tree6. How to alter tree6 while  
'travelling along it's nodes',
without messy referencing as tree6[0][1][0][1][1][1][0].......... ?

Thanx





More information about the Python-list mailing list