我有一个字符串列表,像这样:

X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
Y = [ 0,   1,   1,   0,   1,   2,   2,   0,   1 ]

使用Y中的值对X进行排序以得到以下输出的最短方法是什么?

["a", "d", "h", "b", "c", "e", "i", "f", "g"]

具有相同“键”的元素的顺序并不重要。我可以使用for结构,但我很好奇是否有更短的方法。有什么建议吗?


当前回答

More_itertools有一个并行排序可迭代对象的工具:

鉴于

from more_itertools import sort_together


X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
Y = [ 0,   1,   1,    0,   1,   2,   2,   0,   1]

Demo

sort_together([Y, X])[1]
# ('a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g')

其他回答

另外,如果你不介意使用numpy数组(或者实际上已经在处理numpy数组…),这里有另一个很好的解决方案:

people = ['Jim', 'Pam', 'Micheal', 'Dwight']
ages = [27, 25, 4, 9]

import numpy
people = numpy.array(people)
ages = numpy.array(ages)
inds = ages.argsort()
sortedPeople = people[inds]

我在这里找到的: http://scienceoss.com/sort-one-list-by-another-list/

下面是Whatangs的答案,如果你想获得两个排序的列表(python3)。

X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
Y = [ 0,   1,   1,    0,   1,   2,   2,   0,   1]

Zx, Zy = zip(*[(x, y) for x, y in sorted(zip(Y, X))])

print(list(Zx))  # [0, 0, 0, 1, 1, 1, 1, 2, 2]
print(list(Zy))  # ['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g']

记住Zx和Zy是元组。 我也在想是否有更好的方法来做到这一点。

警告:如果你用空列表运行它,它会崩溃。

这是一个老问题,但我看到的一些答案实际上并不能工作,因为zip是不可编写脚本的。其他答案没有费心导入operator,并在这里提供关于这个模块及其好处的更多信息。

对于这个问题,至少有两个好的习语。从您提供的示例输入开始:

X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
Y = [ 0,   1,   1,   0,   1,   2,   2,   0,   1 ]

使用“装饰-排序-取消装饰”成语

这也被称为schwartzan_transform,得名于R. Schwartz,他在90年代在Perl中推广了这种模式:

# Zip (decorate), sort and unzip (undecorate).
# Converting to list to script the output and extract X
list(zip(*(sorted(zip(Y,X)))))[1]                                                                                                                       
# Results in: ('a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g')

注意,在本例中Y和X是按字典顺序排序和比较的。也就是说,比较第一项(来自Y);如果它们相同,则比较来自X的第二项,依此类推。这可能会创建不稳定的输出,除非您包含字典顺序的原始列表索引,以保持副本的原始顺序。

使用operator模块

这使您可以更直接地控制如何对输入进行排序,因此您可以通过简单地声明排序所依据的特定键来获得排序稳定性。点击这里查看更多示例。

import operator    

# Sort by Y (1) and extract X [0]
list(zip(*sorted(zip(X,Y), key=operator.itemgetter(1))))[0]                                                                                                 
# Results in: ('a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g')

我喜欢有一个排序的下标列表。这样,我可以按照与源列表相同的顺序对任何列表进行排序。一旦你有了一个排序的索引列表,一个简单的列表推导就可以做到:

X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
Y = [ 0,   1,   1,    0,   1,   2,   2,   0,   1]

sorted_y_idx_list = sorted(range(len(Y)),key=lambda x:Y[x])
Xs = [X[i] for i in sorted_y_idx_list ]

print( "Xs:", Xs )
# prints: Xs: ["a", "d", "h", "b", "c", "e", "i", "f", "g"]

注意,排序的索引列表也可以使用numpy.argsort()获得。

另一种选择,结合了几个答案。

zip(*sorted(zip(Y,X)))[1]

为了在python3中工作:

list(zip(*sorted(zip(B,A))))[1]