Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > odd behavior

Reply
Thread Tools

odd behavior

 
 
Greg
Guest
Posts: n/a
 
      11-11-2005
Forgive me, and be kind, as I am just a newby learning this language
out of M.L. Hetland's book. The following behavior of 2.4.1 seems very
strange
>>> x = ['aardvark', 'abalone', 'acme', 'add',

'aerate']
>>> x.sort(key=len)
>>> x

['add', 'acme', 'aerate', 'abalone', 'aardvark']
>>> x.sort(reverse=True)
>>> x

['aerate', 'add', 'acme', 'abalone', 'aardvark']
The function called on line 4, at least to me, should work on x as it
was on line 3, not the previously existing x on line 1. What gives?
By the way these functions do not exist in 2.3.5 so they must be newly
implemented.

 
Reply With Quote
 
 
 
 
Kristian Zoerhoff
Guest
Posts: n/a
 
      11-11-2005
On 11 Nov 2005 11:34:47 -0800, Greg <> wrote:
> Forgive me, and be kind, as I am just a newby learning this language
> out of M.L. Hetland's book. The following behavior of 2.4.1 seems very
> strange
> >>> x = ['aardvark', 'abalone', 'acme', 'add',

> 'aerate']
> >>> x.sort(key=len)
> >>> x

> ['add', 'acme', 'aerate', 'abalone', 'aardvark']
> >>> x.sort(reverse=True)
> >>> x

> ['aerate', 'add', 'acme', 'abalone', 'aardvark']
> The function called on line 4, at least to me, should work on x as it
> was on line 3, not the previously existing x on line 1. What gives?


The key option defaults to an alphabetic sort *every time* you call
sort, so if you want to change this, you must call for your sort key
each time. To do what you want, roll the sorts into one step:

>>> x.sort(key=len, reverse=True)
>>> x

['aardvark', 'abalone', 'aerate', 'acme', 'add']


--
Kristian

kristian.zoerhoff(AT)gmail.com
zoerhoff(AT)freeshell.org
 
Reply With Quote
 
 
 
 
Lee Harr
Guest
Posts: n/a
 
      11-11-2005
On 2005-11-11, Kristian Zoerhoff <> wrote:
> On 11 Nov 2005 11:34:47 -0800, Greg <> wrote:
>> Forgive me, and be kind, as I am just a newby learning this language
>> out of M.L. Hetland's book. The following behavior of 2.4.1 seems very
>> strange
>> >>> x = ['aardvark', 'abalone', 'acme', 'add',

>> 'aerate']
>> >>> x.sort(key=len)
>> >>> x

>> ['add', 'acme', 'aerate', 'abalone', 'aardvark']
>> >>> x.sort(reverse=True)
>> >>> x

>> ['aerate', 'add', 'acme', 'abalone', 'aardvark']
>> The function called on line 4, at least to me, should work on x as it
>> was on line 3, not the previously existing x on line 1. What gives?

>
> The key option defaults to an alphabetic sort *every time* you call
> sort, so if you want to change this, you must call for your sort key
> each time. To do what you want, roll the sorts into one step:
>
>>>> x.sort(key=len, reverse=True)
>>>> x

> ['aardvark', 'abalone', 'aerate', 'acme', 'add']
>
>



.... or just reverse it after:

>>> x.sort(key=len)
>>> x.reverse()
>>> x

['aardvark', 'abalone', 'aerate', 'acme', 'add']

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      11-12-2005
On Fri, 11 Nov 2005 11:34:47 -0800, Greg wrote:

> Forgive me, and be kind, as I am just a newby learning this language
> out of M.L. Hetland's book. The following behavior of 2.4.1 seems very
> strange


>>>> x = ['aardvark', 'abalone', 'acme', 'add', 'aerate']
>>>> x.sort(key=len)
>>>> x

> ['add', 'acme', 'aerate', 'abalone', 'aardvark']
>>>> x.sort(reverse=True)
>>>> x

> ['aerate', 'add', 'acme', 'abalone', 'aardvark']


> The function called on line 4, at least to me, should work on x as it
> was on line 3, not the previously existing x on line 1. What gives?


Why do you think it isn't operating on x as it is? The second sort is
sorting in reverse lexicographic order, which is the result you get.

I'm not running Python 2.4 so I can't test this, but to get the result you
want I guess you want this:

py> x.sort(key=len, reverse=True)
py> x
['aardvark', 'abalone', 'aerate', 'acme', 'add']

or:

py> x.sort(key=len)
py> x.reverse()


--
Steven.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Odd behavior with odd code Michael Speer C Programming 33 02-18-2007 07:31 AM
VERY odd routing behavior when attempting VPN connections over Wifi Robert Gordon Wireless Networking 0 08-25-2005 04:04 PM
Firefox under Linux -- odd behavior Dennis J. Tuchler Firefox 0 07-28-2004 04:05 PM
Odd behavior behind the PIX Charles Haron Cisco 1 04-21-2004 04:05 AM
Odd console behavior on Cat 5005 Mike Voss Cisco 0 11-19-2003 10:49 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57