odd behavior

G

Greg

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.
 
K

Kristian Zoerhoff

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:
['aardvark', 'abalone', 'aerate', 'acme', 'add']
 
L

Lee Harr

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:
['aardvark', 'abalone', 'aerate', 'acme', 'add']


.... or just reverse it after:
['aardvark', 'abalone', 'aerate', 'acme', 'add']
 
S

Steven D'Aprano

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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top