Help, replacing the actual value from a data strucutre in a iterator

george hart gehart24 at yahoo.com
Sat Jan 4 04:42:51 CET 2003


Hello,

My problem is that I have a very complex data structure I need to
iterate over and change certain values to.  I wrote a 'generator' to
iterate over my data structure but when i call the next() method in
the generator object I am returned a value that cannot be used to
modifiy the original data structure.

Here is a simplified version of my problem:

a=[0,1,2,[20,30,40],9,3]
def generator(l):
    for i in l:
        if type(i) is list:
            for q in generator(i):
                yield q
        else:
            yield i

g=generator(a)
o=g.next()
o=100   # where i want this assignment to change a[0] to 100
print a[0]  #thus a[0] would then print out 100


Thanks for any help,
George.




More information about the Python-list mailing list