sort()函数有问题 [英] Problem with the sort() function

查看:93
本文介绍了sort()函数有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个阵列数组,形式为

list = [[3,''fork'',0.3,1 ],[2,''fork,0.1,2],[3,''exec'',0.2,2]]

内置排序(),列表。 sort()对第一个元素进行排序,如果第一个

elts相等,那么它会对第二个元素进行排序等等......但是我真的不想要b / b
不想要如果第一个elts相等的话,搜索第二个elt ...

1-D列表shud留在同一个位置,即我想要排序列表

be [[2,''fork'',0.1,2],[3,''fork,0.3,1],[3,''exec'',0.2,2]而不是

[[2,''fork'',0.1,2],[3,''exec'',0.2,2],[3,''fork,0.3,1]。

我尝试了以下代码:


def mysort(x,y):

返回x [0] -y [0]


list.sort(mysort)


这似乎适用于我输入的一小部分...但不适用于我的

实际输入,其中包含超过2000个子列表(如此显而易见狡猾我无法追踪
每次通过
..)。有人能告诉我我错过了什么吗?

Hi,

I have an array of arrays in the form of
list = [[3,''fork'',0.3,1],[2,''fork,0.1,2],[3,''exec'',0.2,2]]

The in-built sort(),list.sort() sorts on the first element, if the first
elts are equal then it sorts on the second elt and so on...But i really
dont want to search on the second elt if the first elts are equal...the
1-D lists shud be left in the same position i.e. i want the sorted list to
be [[2,''fork'',0.1,2],[3,''fork,0.3,1],[3,''exec'',0.2,2]] and not
[[2,''fork'',0.1,2],[3,''exec'',0.2,2],[3,''fork,0.3,1]].

I tried the following code:

def mysort(x,y):
return x[0]-y[0]

list.sort(mysort)

This somehow seems to work for a small subset of my input...but not for my
real input which has more than 2000 sub-lists(and so obviously i cant trace
each pass..). Can someone tell me what i''m missing?

推荐答案

clementine写道:
clementine wrote:

<我有一个数组的数组,形式为
list = [[3,''fork'',0.3,1],[2,''fork,0.1,2],[3,' 'exec'',0.2,2]]

内置的sort(),list.sort()对第一个元素进行排序,如果第一个元素相等则排序在第二个elt等等......但我真的不想在第二个elt上搜索第一个elts是否相等...
1-D列表shud留在同一个位置即我希望排序列表为[[2,''fork'',0.1,2],[3,''fork,0.3,1],[3,''exec'',0.2, 2]]而不是
[[2,''fork'',0.1,2],[3,''exec'',0.2,2],[3,''fork,0.3,1]] 。
Hi,

I have an array of arrays in the form of
list = [[3,''fork'',0.3,1],[2,''fork,0.1,2],[3,''exec'',0.2,2]]

The in-built sort(),list.sort() sorts on the first element, if the first
elts are equal then it sorts on the second elt and so on...But i really
dont want to search on the second elt if the first elts are equal...the
1-D lists shud be left in the same position i.e. i want the sorted list to
be [[2,''fork'',0.1,2],[3,''fork,0.3,1],[3,''exec'',0.2,2]] and not
[[2,''fork'',0.1,2],[3,''exec'',0.2,2],[3,''fork,0.3,1]].




试试这个:


Py>来自运营商导入项目符号

Py> list = [[3,''fork'',0.3,1],[2,''fork'',0.1,2],[3,''exec'',0.2,2]

Py> list.sort(key = itemgetter(0))

Py> list

[[2,''fork'',0.10000000000000001,2],[3,''fork'',0.29999999999999999,1],[3,''

exec'',0.20000000000000001,2]]


如果不接受''key''参数(即你没有使用Python 2.4),那么你'' l $

需要手动进行装饰:


def mysort(可迭代,cmp =无,键=无,反向=假):

返回其输入的已排序副本

如果sys.version_info> =(2,4):

返回已排序(可迭代,cmp,键,反向)

seq = list(iterable)

如果反向:

seq.reverse()#preserve stability

如果key不是None:

seq = [(key(elem),i,elem)for i,elem in enumerate(seq)]

seq。 sort(cmp)

如果key不是None:

seq = [elem for(key,i,elem)seq]

if反向:

seq.reverse()

返回seq


list = mysort([[3,''for k'',0.3,1],[2,''fork'',0.1,2],[3,''exec'',0.2,2]],

key = lambda x :x [0])

(取自Raymond的代码:
http://mail.python.org/pipermail/pyt...ry/263275.html ) />

干杯,

尼克。


-

Nick Coghlan | nc******@email.com |澳大利亚布里斯班

--------------------------------------- ------------------------
http://boredomandlaziness.skystorm.net



Try this:

Py> from operator import itemgetter
Py> list = [[3,''fork'',0.3,1],[2,''fork'',0.1,2],[3,''exec'',0.2,2]]
Py> list.sort(key=itemgetter(0))
Py> list
[[2, ''fork'', 0.10000000000000001, 2], [3, ''fork'', 0.29999999999999999, 1], [3, ''
exec'', 0.20000000000000001, 2]]

If the ''key'' argument isn''t accepted (i.e. you aren''t using Python 2.4), you''ll
need to do the decoration manually:

def mysort(iterable, cmp=None, key=None, reverse=False):
"return a sorted copy of its input"
if sys.version_info >= (2,4):
return sorted(iterable, cmp, key, reverse)
seq = list(iterable)
if reverse:
seq.reverse() # preserve stability
if key is not None:
seq = [(key(elem), i, elem) for i, elem in enumerate(seq)]
seq.sort(cmp)
if key is not None:
seq = [elem for (key, i, elem) in seq]
if reverse:
seq.reverse()
return seq

list = mysort([[3,''fork'',0.3,1],[2,''fork'',0.1,2],[3,''exec'',0.2,2]],
key=lambda x: x[0])

(Taken from Raymond''s code in:
http://mail.python.org/pipermail/pyt...ry/263275.html)

Cheers,
Nick.

--
Nick Coghlan | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net


Thanx Nick ...我忘了提到即时通讯使用python 2.2和主机

其他东西似乎没有内置功能

:(:(:( ...是否有可能用其他东西替换它?我不想想

模拟它是可行的....

Thanx Nick...I forgot to mention im using python 2.2 and along with a host
of other things it doesnt seem to have the enumarate built in function
:(:(:(...is it possible to replace it by something else? I dont think
simulating it will be feasible....


克莱门汀< ka ********* ******@yahoo.com>写道:
clementine <ka***************@yahoo.com> wrote:
Thanx Nick ...我忘了提到即时通讯使用python 2.2和主机
其他东西它似乎没有有功能内置功能
:(:(:( ...是否有可能用其他东西替换它?我不认为模拟它是可行的......
Thanx Nick...I forgot to mention im using python 2.2 and along with a host
of other things it doesnt seem to have the enumarate built in function
:(:(:(...is it possible to replace it by something else? I dont think
simulating it will be feasible....




这是我准备的一个r:


if sys.version_info< (2,3):

def enumerate(l):

返回拉链(范围(len(l)),l)


对于大型列表而言会有些贬值,相比之下,使用迭代器可以实现



-

\S - si***@chiark.greenend.org.uk - http://www.chaos.org.uk/~sion/

___ | 坦率地说,我对这种或那种企鹅没有任何感情

\ X / | --Arthur C. Clarke

她的努力是什么?ddre hl?heddes b?ce bump bump bump



Here''s one I prepared earlier:

if sys.version_info < (2,3):
def enumerate(l):
return zip(range(len(l)), l)

which will suck somewhat on large lists compared to being able to
do it with iterators.

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomet se bera eadward ofdun hl?ddre heafdes b?ce bump bump bump


这篇关于sort()函数有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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