473,749 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

python 3: sorting with a comparison function

Does Python 3 have no way anymore to sort with a comparison function?

Both [].sort() and sorted() seem to accept only 'key' and 'reverse' arguments,
the 'cmp' argument seems to be gone. Can that be?

Thomas
Oct 9 '08 #1
11 22071
Thomas Heller:
the 'cmp' argument seems to be gone. Can that be?
Yes, that's a wonderful thing, because from the code I see around
99.9% of people see the cmp and just use it, totally ignoring the
presence of the 'key' argument, that allows better and shorter
solutions of the sorting problem. So removing the cmp is the only way
to rub the nose of programmers on the right solution, and it goes well
with the Python "There should be one-- and preferably only one --
obvious way to do it.".

For most of very uncommon situations where key isn't the right thing,
you can use this code by Hettinger:

def cmp2key(mycmp):
"Converts a cmp= function into a key= function"
class K:
def __init__(self, obj, *args):
self.obj = obj
def __cmp__(self, other):
return mycmp(self.obj, other.obj)
return K
s.sort(key=cmp2 key(lambda p, q: cmp(p.lower(), q.lower())))

That code can't be used in one situation: when the array to be sorted
is huge, that situation can be handled by the original true cmp
function, but not by that cmp2key(). But I have met such situation so
far. When you have an huge amount of data, use an external sort, even
Windows has one, even if its usage is a bit tricky (linux sort command
is safer).

Bye,
bearophile
Oct 9 '08 #2
Thomas Heller wrote:
Does Python 3 have no way anymore to sort with a comparison function?

Both [].sort() and sorted() seem to accept only 'key' and 'reverse' arguments,
the 'cmp' argument seems to be gone. Can that be?
Yes. When this was discussed, no one could come up with an actual use
case in which the compare function was not based on a key function.
Calling the key function n times has to be faster than calling a compare
function n to O(nlogn) times with 2 keys computed for each call. The
main counter argument would be if there is no room in memory for the
shadow array of key,index pairs. And that can be at least sometimes
handled by putting the original on disk and sorting an overt key,index
array. Or by using a database.

Oct 9 '08 #3
Thomas Heller wrote:
>Does Python 3 have no way anymore to sort with a comparison function?

Both [].sort() and sorted() seem to accept only 'key' and 'reverse' arguments,
the 'cmp' argument seems to be gone. Can that be?
Terry Reedy schrieb:
Yes. When this was discussed, no one could come up with an actual use
case in which the compare function was not based on a key function.
Calling the key function n times has to be faster than calling a compare
function n to O(nlogn) times with 2 keys computed for each call. The
main counter argument would be if there is no room in memory for the
shadow array of key,index pairs. And that can be at least sometimes
handled by putting the original on disk and sorting an overt key,index
array. Or by using a database.
be************@ lycos.com schrieb:
Yes, that's a wonderful thing, because from the code I see around
99.9% of people see the cmp and just use it, totally ignoring the
presence of the 'key' argument, that allows better and shorter
solutions of the sorting problem. So removing the cmp is the only way
to rub the nose of programmers on the right solution, and it goes well
with the Python "There should be one-- and preferably only one --
obvious way to do it.".

Thanks, I got it now.

Thomas
Oct 10 '08 #4
On 9 Okt., 22:36, bearophileH...@ lycos.com wrote:
Yes, that's a wonderful thing, because from the code I see around
99.9% of people see the cmp and just use it, totally ignoring the
presence of the 'key' argument, that allows better and shorter
solutions of the sorting problem.
Me too because I don't get this:

"key specifies a function of one argument that is used to extract a
comparison key from each list element: key=str.lower. The default
value is None."

Kay
Oct 10 '08 #5
On Oct 10, 8:35 am, Kay Schluehr <kay.schlu...@g mx.netwrote:
On 9 Okt., 22:36, bearophileH...@ lycos.com wrote:
Yes, that's a wonderful thing, because from the code I see around
99.9% of people see the cmp and just use it, totally ignoring the
presence of the 'key' argument, that allows better and shorter
solutions of the sorting problem.

Me too because I don't get this:

"key specifies a function of one argument that is used to extract a
comparison key from each list element: key=str.lower. The default
value is None."

Kay
Don't know if further explanation is needed, but here is the deal:

cmp is a function that receives two values and you return -1, 0 or 1
depending if the first is smaller, equal or bigger. 99% of the time
you will do some operation on the values that come in and then do a if
statement with ">" or "<" and return -1,0,1.

key is a function that receives one value and you return the value
that you would normally compare against.

Let me show an example:
>>data=[(4,'v'),(2,'x') ,(1,'a')]
sorted(data )
[(1, 'a'), (2, 'x'), (4, 'v')]

OK, we sorted the data, but What if we want to sort by the letter
instead of the number? Let's use cmp:
>>def comp(x, y):
key_of_x=x[1]
key_of_y=y[1]
if key_of_x < key_of_y:
return -1
elif key_of_x key_of_y:
return 1
else:
return 0 #key_of_x == key_of_y
>>sorted(data,c mp=comp)
[(1, 'a'), (4, 'v'), (2, 'x')]

Very well, so how do we do this using key?
>>def keyfunc(x):
key_of_x=x[1]
return key_of_x
>>sorted(data,k ey=keyfunc)
[(1, 'a'), (4, 'v'), (2, 'x')]
Same output. Very good.

(Of course a smart python developer would use the operator module so
he doesn't even have to write keyfunc but this was just an example)

In summary to transform most cmp functions to a key function you just
take the code that calculates the first value to be compared and leave
out the rest of the logic.

Hope that was helpful.
Oct 10 '08 #6
Kay Schluehr:
Sometimes it helps when people just make clear how they use technical
terms instead of invoking vague associations.
And generally Python docs can enjoy growing few thousands examples...

Bye,
bearophile
Oct 10 '08 #7
On Oct 10, 12:22*pm, prueba...@latin mail.com wrote:
On Oct 10, 8:35 am, Kay Schluehr <kay.schlu...@g mx.netwrote:
On 9 Okt., 22:36, bearophileH...@ lycos.com wrote:
Yes, that's a wonderful thing, because from the code I see around
99.9% of people see the cmp and just use it, totally ignoring the
presence of the 'key' argument, that allows better and shorter
solutions of the sorting problem.
Me too because I don't get this:
"key specifies a function of one argument that is used to extract a
comparison key from each list element: key=str.lower. The default
value is None."
Kay

Don't know if further explanation is needed, but here is the deal:

cmp is a function that receives two values and you return -1, 0 or 1
depending if the first is smaller, equal or bigger. 99% of the time
you will do some operation on the values that come in and then do a if
statement with ">" or "<" and return -1,0,1.

key is a function that receives one value and you return the value
that you would normally compare against.

Let me show an example:
>data=[(4,'v'),(2,'x') ,(1,'a')]
sorted(data)

[(1, 'a'), (2, 'x'), (4, 'v')]

OK, we sorted the data, but What if we want to sort by the letter
instead of the number? Let's use cmp:
>def comp(x, y):

* * * key_of_x=x[1]
* * * key_of_y=y[1]
* * * if key_of_x < key_of_y:
* * * * return -1
* * * elif key_of_x key_of_y:
* * * * return 1
* * * else:
* * * * return 0 #key_of_x == key_of_y
>sorted(data,cm p=comp)

[(1, 'a'), (4, 'v'), (2, 'x')]

Very well, so how do we do this using key?
>def keyfunc(x):

* * * key_of_x=x[1]
* * * return key_of_x
>sorted(data,ke y=keyfunc)

[(1, 'a'), (4, 'v'), (2, 'x')]

Same output. Very good.

(Of course a smart python developer would use the operator module so
he doesn't even have to write keyfunc but this was just an example)
IIRC, the return values are not limited to -1, 0, and 1, but are more
like "any value less than 0", 0, and "any value greater than 0". This
allows you to implement numeric cmp routines as:

def cmp(x,y):
return x-y

or just:

cmp = lambda x,y: x-y

-- Paul
Oct 10 '08 #8
On 10 Okt., 20:38, bearophileH...@ lycos.com wrote:
Kay Schluehr:
Sometimes it helps when people just make clear how they use technical
terms instead of invoking vague associations.

And generally Python docs can enjoy growing few thousands examples...
Cleaning up and extending documentation is a large community effort
that requires an informational PEP for guidelines and management
support by the python-dev leads. The official documentation is ad hoc
and just about better than nothing. A Python documentation guideline
might also have positive impact on 3rd party package authors like us.

Generally Python has become a very well managed project. I hope the
docs as well as the stdlib will become the major concerns of Python
3.1.
Oct 10 '08 #9
be************@ lycos.com schrieb:
Kay Schluehr:
>Sometimes it helps when people just make clear how they use technical
terms instead of invoking vague associations.

And generally Python docs can enjoy growing few thousands examples...
Well, that may not be necessary. But I think that a clear example how to use
the 'key=' parameter in the sort() and sorted() method/function is badly needed.

Thomas
Oct 10 '08 #10

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

Similar topics

40
2984
by: Xah Lee | last post by:
is it possible in Python to create a function that maintains a variable value? something like this: globe=0; def myFun(): globe=globe+1 return globe
11
2146
by: David Rasmussen | last post by:
I want to use sort() and supply my own comparison function, like bool lessThan(const S& a, const S& b) { return value(a) < value(b); } and then sort by: sort(a.begin(), a.end(), lessThan);
6
4467
by: aurgathor | last post by:
Howdy, How do I pass some function a generic comparison function? I figured out one non-generic case, but since this code got parameter declarations in two places, it's obviously not generic. TIA #include <stdio.h>
14
5019
by: Spoon | last post by:
Hello, I've come across the following comparison function used as the 4th parameter in qsort() int cmp(const void *px, const void *py) { const double *x = px, *y = py; return (*x > *y) - (*x < *y); }
2
6776
by: eastern_strider | last post by:
I'm running into problems about defining a comparison function for a map which has a user defined key. For example: class Key { public: string name; int number; Key (na, nu) : name (na), number (nu) {} bool operator< (const Key &key) const; //my question is how to
0
2577
by: SvenMathijssen | last post by:
Hi, I've been wrestling with a problem for some time that ought to be fairly simple, but turns out to be very difficult for me to solve. Maybe someone here knows the answer. What I try to do is sort the records in a plain-text index file based on certain columns. The index file consists of records and fields within the records. The individual fields are separated by semicolons, the records by newlines. The index file is loaded into memory...
5
3905
by: fade | last post by:
Good afternoon, I need some advice on the following: I've got a class that has a member std::vector<CStringm_vFileName and a member CString m_path; The vector contains a bunch of filenames with no path included (no C:\...) eg: my_file2.jpg, my_file1.bmp, etc... and m_path stores the path, eg: C:\folder1 I want to sort this vector according to different criterion, such as
18
6506
by: PicO | last post by:
how can i make a set with comparison function ? all i know that i can make a map with comparison function like this struct strCmp { bool operator()( const char* s1, const char* s2 ) const { return strcmp( s1, s2 ) < 0; } };
3
1377
by: laredotornado | last post by:
Hi, I'm using php 4.4.4. Given two variables, $dir1 = "/usr/local/apache2/htdocs/" $dir2 = "/usr/local/apache2/htdocs" What is a comparison function I could write that would say these two directories are the same? A straight string comparison would not work above.
0
8833
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9568
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9389
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8257
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6801
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6079
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3320
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2218
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.