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

Terry Reedy tjreedy at udel.edu
Sat Jan 4 02:21:32 EST 2003


"george hart" <gehart24 at yahoo.com> wrote in message
news:c490a7ae.0301031942.5f39a6a8 at posting.google.com...
> 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 is now bound to 0

> o=100   # where i want this assignment to change a[0] to 100

No way.  You are simply rebinding 'o' to a new value.  Just write
'a[0] = 100'!

> print a[0]  #thus a[0] would then print out 100

Terry J. Reedy






More information about the Python-list mailing list