使用另一个列表中的值对一个列表进行排序 [英] sort one list using the values from another list

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

问题描述

您好,


我有两个列表,一个包含字符串(实际上是文件名),另一个包含实数

等级,如:


A = [''你好'''''有'',''这'',''那个'']

B = [ 3,4,2,5]

我想使用B中的值对列表A进行排序,因此结果将在此示例中,


A = [''this'',''hello'',''there'',''that'']

列表中的排序方法进行就地排序。有没有办法在这里做我想要的?

谢谢,


Brian Blais


-

-----------------

bb****@bryant.edu
http:// web.bryant.edu/~bblais

Hello,

I have two lists, one with strings (filenames, actually), and one with a real-number
rank, like:

A=[''hello'',''there'',''this'',''that'']
B=[3,4,2,5]

I''d like to sort list A using the values from B, so the result would be in this example,

A=[''this'',''hello'',''there'',''that'']

The sort method on lists does in-place sorting. Is there a way to do what I want here?
thanks,

Brian Blais

--
-----------------

bb****@bryant.edu
http://web.bryant.edu/~bblais

推荐答案

Brian Blais写道:
Brian Blais wrote:
你好,

我有两个列表,一个是字符串(实际上是文件名),另一个是
实数等级,如:

A = [''你好'',''有'',''这'',''那个'']
B = [3,4,2,5]

我想使用B中的值对列表A进行排序,因此在这个例子中结果将是

A = [''this'',''hello'' ,''那里',''那个'']


这里有两种方法:
Hello,

I have two lists, one with strings (filenames, actually), and one with a
real-number
rank, like:

A=[''hello'',''there'',''this'',''that'']
B=[3,4,2,5]

I''d like to sort list A using the values from B, so the result would be
in this example,

A=[''this'',''hello'',''there'',''that'']
Here are two ways:
A = [ '你好'',''那里',''这个'',''那个'']
B = [3,4,2,5]
拉链(*排序(拉链) ,A)))[1]
(''this'',''你好'',''有',''那'')

[a for b, a in sorted(zip(B,A))]
A=[''hello'',''there'',''this'',''that'']
B=[3,4,2,5]
zip(*sorted(zip(B,A)))[1] (''this'', ''hello'', ''there'', ''that'')
[a for b,a in sorted(zip(B,A))]



[''this'',''hello'',''there'' ,''那个'']


我更喜欢第二个,我认为它更具可读性,会少用

内存(没有在最后一步创建一个新的列表B)并且在我的计算机上更快地获得



D:\Projects \CB> ; python -m timeit -s

" A = [''hello'',''there'',''this'',''that'']; B = [3, 4,2,5] QUOT; " zip(* sorted(zip(B,A)))[1]"

100000循环,最佳3:每循环使用6.29


D:\Projects\CB> python -m timeit -s

" A = [''你好'',''有'',''这个'',''那个'']; B = [3,4,2,5] QUOT; [a for b,a in

sorted(zip(B,A))]"

100000循环,最佳3:5.53 usec每循环


(今天早上我很无聊:-)


使用关键参数进行排序可能有一种聪明的方法

但我无法想到它......
列表上的排序方法可以进行就地排序。有没有办法呢
我想要什么?


[''this'', ''hello'', ''there'', ''that'']

I prefer the second one, I think it is more readable, will use less
memory (doesn''t have to create a new list B in the final step) and it''s
even faster on my computer:

D:\Projects\CB>python -m timeit -s
"A=[''hello'',''there'',''this'',''that''];B=[3,4,2,5]" "zip(*sorted(zip(B,A)))[1]"
100000 loops, best of 3: 6.29 usec per loop

D:\Projects\CB>python -m timeit -s
"A=[''hello'',''there'',''this'',''that''];B=[3,4,2,5]" "[a for b,a in
sorted(zip(B,A))]"
100000 loops, best of 3: 5.53 usec per loop

(I''m bored this morning :-)

There''s probably a clever way to do it using the key parameter to sort
but I can''t think of it...
The sort method on lists does in-place sorting. Is there a way to do
what I want here?




上面的例子没有排序到位,如果你想要它到位使用

A [:] = [a for b,a in in sorted(zip(B,A))]


Kent



The example above does not sort in place, if you want it to be in place use
A[:] = [a for b,a in sorted(zip(B,A))]

Kent


Brian Blais写道:
Brian Blais wrote:
你好,

我有两个列表,一个是字符串(实际上是文件名),另一个是
真实数字等级,如:

A = [''你好'',''有'',''这个'',''那个''] B = [3,4,2,5]

我想使用B中的值对列表A进行排序,因此在这个例子中结果将是


A = [''this'',''hello'',''there'',''that'']

列表上的排序方法就地完成排序。有什么方法可以做我想要的吗?
Hello,

I have two lists, one with strings (filenames, actually), and one with a
real-number
rank, like:

A=[''hello'',''there'',''this'',''that'']
B=[3,4,2,5]

I''d like to sort list A using the values from B, so the result would be
in this example,

A=[''this'',''hello'',''there'',''that'']

The sort method on lists does in-place sorting. Is there a way to do
what I want here?




如果A没有重复的元素,你可以创建一个哈希映射A'的

元素各自的优先级,然后提供一个访问哈希的排序标准

。或者,您可以执行以下操作:


来自运算符导入项目符号

result = map(itemgetter(0),已排序(zip(A,B)) ,key = itemgetter(1)))



If A has no duplicate elements, you could create a hash mapping A''s
elements to their respective precedences, then provide a sort criterion
that accessed the hash. Alternatively, you could do something like this:

from operator import itemgetter
result = map(itemgetter(0), sorted(zip(A, B), key=itemgetter(1)))


Brian Blais< bb **** @ bryant.edu>写道:
Brian Blais <bb****@bryant.edu> wrote:
你好,

我有两个列表,一个是字符串(实际上是文件名),另一个是
实数等级,比如:

A = [''你好'',''有'',''这'',''那个'']
B = [3,4,2] ,5]

我想用B中的值对列表A进行排序,所以结果将在这个例子中,

A = [ ''this'',''hello'',''there'',''that'']

列表上的排序方法可以进行就地排序。有什么办法可以做我想要的吗?
Hello,

I have two lists, one with strings (filenames, actually), and one with a
real-number rank, like:

A=[''hello'',''there'',''this'',''that'']
B=[3,4,2,5]

I''d like to sort list A using the values from B, so the result would be in
this example,

A=[''this'',''hello'',''there'',''that'']

The sort method on lists does in-place sorting. Is there a way to do what
I want here?




当然,很多方式,主要是基于装饰 - 排序 - 未装饰的方式

idiom及其化身为key =函数的可选参数

排序和列表方法排序。我相信在你的情况下,一个更明确的DSU是理想的(更清晰,但可能有点慢):


_aux = zip(B,A)

_aux.sort()

A [:] = [a for b,a in _aux]


扭曲东西到使用speedy key =方法似乎更扭曲

并且在这种情况下不太通用,而且它还需要一个辅助结构

(以避免因重复而减慢速度.index调用)但是,如果你需要考虑B有一些重复项目的可能性,那么事情

可能会有所不同,并且非常关心这种情况不会导致结果在

比较A的项目。然后上面的方法必须改为

,例如,改为:


_aux = zip(B,枚举(A))

_aux.sort()
$ _ $ b a [:] = [a for(b,(i,a))in _aux ]


我还添加了一对冗余的括号,以便更好地使用_aux
项目的嵌套结构秒。当然,每个

这些都有一个使用sorted而不是sort的变体,对于那些你可以使用来自itertools的izip而不是内置zip的那些,并且

一个单一陈述中的所有内容等等。

Alex



Sure, many ways, mostly ones based on the "Decorate-Sort-Undecorate"
idiom and its incarnation as the key= optional argument to function
sorted and list method sort. I believe that a more explicit DSU is
ideal (clearer, though maybe a tad slower) in your case:

_aux = zip(B, A)
_aux.sort()
A[:] = [a for b, a in _aux]

Twisting things to use the speedy key= approach seems more "contorted"
and less general in this case, and it also needs an auxiliary structure
(to avoid slowing things down by repeated .index calls) However, things
may be different if you need to consider the possibility that B has some
duplicate items, and deeply care for such a case NOT to result in
comparisons of items of A. The above approach would then have to be
changed, e.g., to:

_aux = zip(B, enumerate(A))
_aux.sort()
A[:] = [a for (b, (i, a)) in _aux]

where I''ve also added a pair of redundant parentheses to make the
nesting structure of items of _aux more obvious. Of course, each of
these has a variant using sorted instead of sort, and for those you
could use izip from itertools rather than built-in zip, and do
everything within one single statement, etc, etc.
Alex


这篇关于使用另一个列表中的值对一个列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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