对多个值进行排序,一些是递增的,一些是递减的 [英] Sorting on multiple values, some ascending, some descending

查看:101
本文介绍了对多个值进行排序,一些是递增的,一些是递减的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功使用
中描述的排序lambda构造 http://mail.python.org/pipermail/pyt...il/377443.html

但是,我该怎么做呢进一步使得某些值可以按升序排序而其他值下降?很容易,如果排序值

是数字(只是否定值),但文本怎么样?


是定义外部排序函数的唯一选项通过列表循环

并一次比较一个值?

I have successfully used the sort lambda construct described in
http://mail.python.org/pipermail/pyt...il/377443.html.
However, how do I take it one step further such that some values can be
sorted ascending and others descending? Easy enough if the sort values
are numeric (just negate the value), but what about text?

Is the only option to define an external sorting function to loop
through the list and perform the comparisons one value at a time?

推荐答案



dwelden写道:

dwelden wrote:

我已经成功使用了
中描述的排序lambda结构 http://mail.python.org/pipermail/pyt...il/377443.html 。

但是,我如何更进一步,以便某些值可以是

按升序排序而其他值是降序?很容易,如果排序值

是数字(只是否定值),但文本怎么样?


是定义外部排序函数的唯一选项通过列表循环

并一次执行一个值的比较?
I have successfully used the sort lambda construct described in
http://mail.python.org/pipermail/pyt...il/377443.html.
However, how do I take it one step further such that some values can be
sorted ascending and others descending? Easy enough if the sort values
are numeric (just negate the value), but what about text?

Is the only option to define an external sorting function to loop
through the list and perform the comparisons one value at a time?



最简单的方法是利用排序稳定性并进行连续排序

。例如,按主键升序排序和

a二级密钥降序:


L.sort(key = lambda r:r.secondary,reverse =真的)

L.sort(关键= lambda r:r.primary)


一种不那么通用的技术是以一种反转方式转换字段/>
他们的比较顺序:


L.sort(key = lambda r:( - r.age,r.height))#排序下降年龄

和升序高度

雷蒙德

The simplest way is to take advantage of sort-stability and do
successive sorts. For example, to sort by a primary key ascending and
a secondary key decending:

L.sort(key=lambda r: r.secondary, reverse=True)
L.sort(key=lambda r: r.primary)

A less general technique is to transform fields in a way that reverses
their comparison order:

L.sort(key=lambda r: (-r.age, r.height)) # sorts descending age
and ascending height
Raymond


2007年3月3日星期三上午10:48 - 0800, dwelden写道:
On Wed, 2007-01-03 at 10:48 -0800, dwelden wrote:

我已成功使用
中描述的排序lambda结构 http://mail.python.org/pipermail/pyt...il/377443.html

但是,我如何更进一步,以便某些值可以是

按升序排序而其他值是降序?很容易,如果排序值

是数字(只是否定值),但文本怎么样?


是定义外部排序函数的唯一选项通过列表循环

并一次执行一个值的比较?
I have successfully used the sort lambda construct described in
http://mail.python.org/pipermail/pyt...il/377443.html.
However, how do I take it one step further such that some values can be
sorted ascending and others descending? Easy enough if the sort values
are numeric (just negate the value), but what about text?

Is the only option to define an external sorting function to loop
through the list and perform the comparisons one value at a time?



如果通过外部排序功能你的意思是一个自定义比较函数

传递给cmp参数排序,然后那是'一个选项,但不是

只有一个。


如果你事先知道哪些列包含字符串,你可以编写一个

函数,它对字符串进行操作并且严格单调地减少
,即它表现为f(x)< f(y)当且仅当x y。

然后可以在排序键中使用此函数对字符串

列按降序排序。以下函数可以解决问题:


def antistring(x):

返回[256-c(c)for c in x] + [257]


(通过精力充沛的手工证明。)


最后,如果你的阵列足够小,你可以容忍
$ b多次通过$ b性能打击,你可以利用

sort()保证在最近的版本
CPython中保持稳定的事实。将n列上的排序拆分为单独的

列中的n个单独排序,并使用''reverse''参数指定是向前还是向后排序




希望这会有所帮助,


Carsten。

If by "external sorting function" you mean a custom comparison function
to pass to sort as the cmp argument, then that''s one option, but not the
only one.

If you know in advance which columns contain strings, you could write a
function that operates on strings and is strictly monotonically
decreasing, i.e. it behaves such that f(x) < f(y) if and only if x y.
This function could then be used in the sort key for sorting on a string
column in descending order. The following function does the trick:

def antistring(x):
return [256-ord(c) for c in x]+[257]

(Proof by vigorous handwaving.)

Lastly, if your array is small enough that you can tolerate the
performance hit of multiple passes, you could exploit the fact that
sort() is guaranteed to be stable in sufficiently recent releases of
CPython. Split your sort on n columns into n separate sorts on a single
column each, and use the ''reverse'' parameter to specify whether to sort
forward or backwards.

Hope this helps,

Carsten.


Raymond Hettinger:
Raymond Hettinger:

最简单的方法是利用排序稳定性并进行连续排序

。例如,按主键升序排序和

a二级密钥降序:

L.sort(key = lambda r:r.secondary,reverse = True)

L.sort(key = lambda r:r.primary)
The simplest way is to take advantage of sort-stability and do
successive sorts. For example, to sort by a primary key ascending and
a secondary key decending:
L.sort(key=lambda r: r.secondary, reverse=True)
L.sort(key=lambda r: r.primary)



这可能是更快更简单的方法。

以下解决方案可能很慢,占用大量内存,并且它没有经过多少测试,所以它可能也有问题,但它显示了我的想法,并且错误可以

修复:


类逆变器:

def __init __(自我,项目,逆转=假):

self.item = item

self.reversed = reverse

def __cmp __(self,other):

if self.reversed:

返回cmp(other.item,self.item)

else:

返回cmp(self.item,other.item)


data = [[1,a,b,[1,b,d],[1,d,a ;]]

reverses = [True,False,False]


打印排序(数据, key = lambda subseq:map(Inverter,subseq,reverses))


再见,

。熊市场

That''s probably the faster and simpler way.
The following solution is probably slow, memory-hungry, and it''s not
tested much so it may be buggy too, but it shows my idea, and bugs can
be fixed:

class Inverter:
def __init__(self, item, reversed=False):
self.item = item
self.reversed = reversed
def __cmp__(self, other):
if self.reversed:
return cmp(other.item, self.item)
else:
return cmp(self.item, other.item)

data = [[1, "a", "b"], [1, "b", "d"], [1, "d", "a"]]
reverses = [True, False, False]

print sorted(data, key=lambda subseq: map(Inverter, subseq, reverses))

Bye,
bearophile


这篇关于对多个值进行排序,一些是递增的,一些是递减的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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