python 3:使用比较函数进行排序 [英] python 3: sorting with a comparison function

查看:85
本文介绍了python 3:使用比较函数进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 3是否已经无法使用比较函数进行排序?


[] .sort()和sorted()似乎只接受''key''和''反向''论证,

''cmp''论证似乎消失了。可以吗?


Thomas

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

推荐答案

Thomas Heller:
Thomas Heller:

''cmp''参数似乎消失了。可以吗?
the ''cmp'' argument seems to be gone. Can that be?



是的,这是一件好事,因为从我看到的代码来看

99.9%的人看到cmp并且只是使用它完全忽略了''key''论证的存在,它允许更好和更短的解决问题的解决方案。所以删除cmp是唯一的方法

在正确的解决方案上揉搓程序员的鼻子,并且很顺利

使用Python应该有一个 - 最好只有一个 -

显而易见的方法。


对于大多数非常罕见的情况,关键不是正确的事情,

你可以使用Hettinger的这段代码:


def cmp2key(mycmp):

"转换cmp = function进入一个键=函数

类K:

def __init __(self,obj,* args):

self.obj = obj

def __cmp __(自我,其他):

返回mycmp(self.obj,other.obj)

返回K

s.sort(key = cmp2key(lambda p,q:cmp(p.lower(),q.lower())))


该代码不能成为在一种情况下使用:当要排序的数组

很大时,这种情况可以由原来的真正的cmp

函数处理,但不能由cmp2key()处理。但我遇到了这样的情况,所以

远。当你有大量数据时,使用外部排序,即使它有一个,即使它的使用有点棘手(Linux排序命令

更安全) 。


再见,

bearophile

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=cmp2key(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


Thomas Heller写道:
Thomas Heller wrote:

Python 3是否已经无法使用比较函数进行排序?


[] .sort()和sorted()似乎只接受''关键''和''反向''论点,

''cmp''论证似乎消失了。可以吗?
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?



是的。讨论这个问题时,没有人能够提出实际使用

的情况,其中比较功能不是基于关键功能。

调用关键功能n次必须比调用比较

函数n到O(nlogn)次更快,每次调用计算2个密钥。

主计数器参数将是内存中没有空间用于关键索引对的

阴影数组。而且至少有时可以通过将原件放在磁盘上并对一个公开的键进行排序来处理
,索引

数组。或者使用数据库。

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.


Thomas Heller写道:
Thomas Heller wrote:

> Python 3是否已经无法使用比较函数进行排序?

两个[] .sort()和sorted()似乎只接受''key''和''反向''论点,
''cmp''论证似乎已经消失了。可以吗?
>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:

Terry Reedy schrieb:


是的。讨论这个问题时,没有人能够提出实际使用

的情况,其中比较功能不是基于关键功能。

调用关键功能n次必须比调用比较

函数n到O(nlogn)次更快,每次调用计算2个密钥。

主计数器参数将是内存中没有空间用于关键索引对的

阴影数组。而且至少有时可以通过将原件放在磁盘上并对一个公开的键进行排序来处理
,索引

数组。或者使用数据库。
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.

成为********* ***@lycos.com schrieb:

be************@lycos.com schrieb:


是的,这是一件好事,因为从我看到的代码来看

99.9%的人看到cmp并且只是使用它,完全忽略了''key''论证的存在,这允许更好更短的

解决方案排序问题。所以删除cmp是唯一的方法

在正确的解决方案上揉搓程序员的鼻子,并且很顺利

使用Python应该有一个 - 并且最好只有一个 -

显而易见的方法。
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.".



谢谢,我现在知道了。


Thomas


Thanks, I got it now.

Thomas


这篇关于python 3:使用比较函数进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆