This indeed fixes the problem, and clarifies some other things for me
as well.
Thanks for your quick response Jonathan.
Ben
On Jan 9, 11:59 pm, Jonathan Bober <[EMAIL PROTECTED]> wrote:
> Hi.
>
> short answer: immediately after the line
>
> while i < 4:
>
> add the line
>
> P = [0,0]
>
> Your problem seems to be that when you put something in a list, python
> does not make a copy of it, but instead stores that actual object in the
> list. So the line
>
> pts.append(P)
>
> appends the object P (or, rather, the object that P references) to the
> list pts. Thus, when you change P, you also 'change' the list. (A better
> way to think of it, though, is that you don't actually change the list
> by changing P. Instead, you might think of the list as eventually
> looking like
>
> [P, P, P, ..., P]
>
> because all you did was append the same object a bunch of times, so of
> course all of the entries in pts are the same at the end.)
>
> The line
>
> P = [0,0]
>
> creates a new list (which P now references). Putting that line in the
> while loop should fix the problem because you will be adding a different
> object to the list each time.
>
> A simpler example is
>
> sage: x = ['something']
> sage: y = x # now x and y are the SAME OBJECT
> sage: x
> ['something']
> sage: y
> ['something']
> sage: x[0] = 'something else'
> sage: y # since x and y are the same object, they have the same contents
> ['something else']
> sage: x = ['something else again'] # now x references a new object,
> sage: y # so y doesn't change
> ['something else']
>
> -bober
>
> On Wed, 2008-01-09 at 21:25 -0800, benjamin antieau wrote:
> > Code for the worksheet attached below.
>
> > There must surely be a simple answer to this problem, but I have not
> > been able to figure it out. I loop through i,j print the list
> > [i,j], and append the list to pts. However, once appended to points
> > something goes wrong, and all that points sees are the constant lists
> > [3,3]. I have changed the various constants and such, but I am at a
> > loss to explain why this is happening.
>
> > What I need is for pts to contain the correct information. Any help
> > would really be appreciated.
>
> > Ben
>
> > {{{id=125|
> > P=[0,0]
> > i=0
> > pts=[]
> > while i<4:
> > P[0]=i
>
> > j=0
> > while j<4:
> > P[1]=j
> > print P
> > pts.append(P)
> > j+=1
> > i+=1
> > pts
> > ///
> > [0, 0]
> > [0, 1]
> > [0, 2]
> > [0, 3]
> > [1, 0]
> > [1, 1]
> > [1, 2]
> > [1, 3]
> > [2, 0]
> > [2, 1]
> > [2, 2]
> > [2, 3]
> > [3, 0]
> > [3, 1]
> > [3, 2]
> > [3, 3]
> > [[3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3,
> > 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3]]
> > }}}
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~----------~----~----~----~------~----~------~--~---