"Wayne Watson" <[EMAIL PROTECTED]> wrote
I was surprised to find the result below.
Well done, you just found one of the standard beginners gotchas!
a =[4,2,5,8]
b = a
This makes b refer to the *same list* as a.
a.sort()
This sorts the list contents in-place
a
[2, 4, 5, 8]
As sh
Solution 1:
>>> a=[2,3,1,4]
>>> b=a[:]
>>> a
[2, 3, 1, 4]
>>> b
[2, 3, 1, 4]
>>> a.sort()
>>> a
[1, 2, 3, 4]
>>> b
[2, 3, 1, 4]
>>>
Solution 2:
>>> from copy import deepcopy
>>> a=[2,1,3,4]
>>> b=deepcopy(a)
>>> a
[2, 1, 3, 4]
>>> b
[2, 1, 3, 4]
>>> a.sort()
>>> a
[1, 2, 3, 4]
>>> b
[2, 1, 3, 4]
On Wed, Sep 17, 2008 at 3:30 PM, Wayne Watson
<[EMAIL PROTECTED]>wrote:
> I'm using Python 2.4 in Win XP. I was surprised to find the result below.
>
> >>> a =[4,2,5,8]
> >>> b = a
> >>> a.sort()
> >>> a
> [2, 4, 5, 8]
> >>> b
> [2, 4, 5, 8]
>
> b no longer has the same value as it began. Apparen
On Wed, Sep 17, 2008 at 3:30 PM, Wayne Watson
<[EMAIL PROTECTED]> wrote:
> I'm using Python 2.4 in Win XP. I was surprised to find the result below.
>
a =[4,2,5,8]
b = a
This binds the name "b" to the same object that "a" is bound to.
a.sort()
a
> [2, 4, 5, 8]
b
> [2, 4,
On Wed, Sep 17, 2008 at 3:30 PM, Wayne Watson
<[EMAIL PROTECTED]> wrote:
> I'm using Python 2.4 in Win XP. I was surprised to find the result below.
>
a =[4,2,5,8]
b = a
a.sort()
a
> [2, 4, 5, 8]
b
> [2, 4, 5, 8]
>
> b no longer has the same value as it began. Apparently to
If you use sort(a), it will be ok.
>>>a =[4,2,5,8]
>>>b=a
>>> sort(a)
: array([2, 4, 5, 8])
>>> b
[4, 2, 5, 8]
>>> a
[4, 2, 5, 8]
On Wed, Sep 17, 2008 at 12:30 PM, Wayne Watson <[EMAIL PROTECTED]
> wrote:
> I'm using Python 2.4 in Win XP. I was surprised to find the result below.
>
> >>> a
Title: Signature.html
I'm using Python 2.4 in Win XP. I was surprised to find the result
below.
>>> a =[4,2,5,8]
>>> b = a
>>> a.sort()
>>> a
[2, 4, 5, 8]
>>> b
[2, 4, 5, 8]
b no longer has the same value as it began. Apparently to prevent sort
from making it the same I have to resort to copy