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

Simon Burton simonb at webone.com.au
Sat Jan 4 07:51:36 EST 2003


On Sat, 04 Jan 2003 03:31:06 +0000, george hart wrote:

>> 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'!
> 
> Hi,
> 
> I'm sorry, I think my example was not very good.  I can't just write
> 'a[0]' because the *real* data structure I am working with is very
> complex and nested.  It would be more like a[1][10][20][40] etc..
> 
> I am a relatively new to python so I am probably trying solve the
> problem the way I would in C (it would be nice to have pointers right
> now :-).   The returned value from the .next() method appears to be
> pointing to the same memory address as the respective element in the
> data structure.  It seems logical that there should be an easy way to
> modify it.
> 
> Plus, there must be a way to do this?  What is the point of being able
> use a generator to iterate over a structure if you can't modify any of
> the values?
> 
> 

but consider; when one is modifying the elements of a sequence
one must count through indexes, not the items (using an iterator):

for i in alist:
  i=1 # so what

for i in range(len(alist)):
  alist[i]=1 # ok now we are doing something






More information about the Python-list mailing list