Re: [Tutor] Sort Output

2008-09-17 Thread Alan Gauld
"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

Re: [Tutor] Sort Output

2008-09-17 Thread Shantanoo Mahajan
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]

Re: [Tutor] Sort Output

2008-09-17 Thread greg whittier
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

Re: [Tutor] Sort Output

2008-09-17 Thread Jerry Hill
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,

Re: [Tutor] Sort Output

2008-09-17 Thread Kent Johnson
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

Re: [Tutor] Sort Output

2008-09-17 Thread Shulin Zhuang
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

[Tutor] Sort Output

2008-09-17 Thread Wayne Watson
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