473,396 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

odd behavior

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.

Nov 11 '05 #1
3 1193
On 11 Nov 2005 11:34:47 -0800, Greg <gs*******@gmail.com> 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
Nov 11 '05 #2
On 2005-11-11, Kristian Zoerhoff <kr***************@gmail.com> wrote:
On 11 Nov 2005 11:34:47 -0800, Greg <gs*******@gmail.com> 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']

Nov 11 '05 #3
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.

Nov 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Dave Rahardja | last post by:
Does the C++ standard specify the behavior of floating point numbers during "exceptional" (exceptional with respect to floating point numbers, not exceptions) conditions? For example: double...
19
by: E. Robert Tisdale | last post by:
In the context of the comp.lang.c newsgroup, the term "undefined behavior" actually refers to behavior not defined by the ANSI/ISO C 9 standard. Specifically, it is *not* true that "anything can...
23
by: Ken Turkowski | last post by:
The construct (void*)(((long)ptr + 3) & ~3) worked well until now to enforce alignment of the pointer to long boundaries. However, now VC++ warns about it, undoubtedly to help things work on 64...
38
by: Steven Bethard | last post by:
> >>> aList = > >>> it = iter(aList) > >>> zip(it, it) > > That behavior is currently an accident. >http://sourceforge.net/tracker/?group_id=5470&atid=105470&func=detail&aid=1121416
7
by: Mike Livenspargar | last post by:
We have an application converted from v1.1 Framework to v2.0. The executable references a class library which in turn has a web reference. The web reference 'URL Behavior' is set to dynamic. We...
66
by: gyan | last post by:
Hi All, I am using sprintf and getting starnge output in following case char temp_rn; memset(temp_rn,'\0',12); sprintf(temp_rn,"0%s",temp_rn); the final value in temp_rn is 00 how it...
12
by: Rajesh S R | last post by:
Can anyone tell me what is the difference between undefined behavior and unspecified behavior? Though I've read what is given about them, in ISO standards, I'm still not able to get the...
28
by: v4vijayakumar | last post by:
#include <string> #include <iostream> using namespace std; int main() { string str; str.resize(5); str = 't';
35
by: bukzor | last post by:
I've found some bizzare behavior when using mutable values (lists, dicts, etc) as the default argument of a function. I want to get the community's feedback on this. It's easiest to explain with...
33
by: coolguyaroundyou | last post by:
Will the following statement invoke undefined behavior : a^=b,b^=a,a^=b ; given that a and b are of int-type ?? Be cautious, I have not written a^=b^=a^=b ; which, of course, is undefined....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.