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

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结构,但我很好奇是否有更短的方法。有什么建议吗?


最短的代码

[x for _, x in sorted(zip(Y, X))]

例子:

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

Z = [x for _,x in sorted(zip(Y,X))]
print(Z)  # ["a", "d", "h", "b", "c", "e", "i", "f", "g"]

一般来说

[x for _, x in sorted(zip(Y, X), key=lambda pair: pair[0])]

解释道:

压缩这两个列表。 使用sorted()根据zip文件创建一个新的排序列表。 使用列表推导式从已排序、压缩的列表中提取每对的第一个元素。

有关如何设置\use key参数以及一般的排序函数的更多信息,请参阅以下内容。



对我来说,最明显的解决方案是使用关键字arg。

>>> X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
>>> Y = [ 0,   1,   1,    0,   1,   2,   2,   0,   1]
>>> keydict = dict(zip(X, Y))
>>> X.sort(key=keydict.get)
>>> X
['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g']

注意,如果你愿意,你可以把它缩短为一行代码:

>>> X.sort(key=dict(zip(X, Y)).get)

正如穆文敏(Wenmin Mu)和杰克·彭(Jack Peng)指出的,这假设X中的值都是不同的。这很容易用索引列表来管理:

>>> Z = ["A", "A", "C", "C", "C", "F", "G", "H", "I"]
>>> Z_index = list(range(len(Z)))
>>> Z_index.sort(key=keydict.get)
>>> Z = [Z[i] for i in Z_index]
>>> Z
['A', 'C', 'H', 'A', 'C', 'C', 'I', 'F', 'G']

由于Whatang所描述的装饰-排序-不装饰方法稍微简单一些,并且适用于所有情况,因此在大多数情况下它可能更好。(这是一个非常古老的答案!)


把两个列表压缩在一起,排序,然后取你想要的部分:

>>> yx = zip(Y, X)
>>> yx
[(0, 'a'), (1, 'b'), (1, 'c'), (0, 'd'), (1, 'e'), (2, 'f'), (2, 'g'), (0, 'h'), (1, 'i')]
>>> yx.sort()
>>> yx
[(0, 'a'), (0, 'd'), (0, 'h'), (1, 'b'), (1, 'c'), (1, 'e'), (1, 'i'), (2, 'f'), (2, 'g')]
>>> x_sorted = [x for y, x in yx]
>>> x_sorted
['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g']

把这些结合起来得到:

[x for y, x in sorted(zip(Y, X))]

Zip,按第二列排序,返回第一列。

zip(*sorted(zip(X,Y), key=operator.itemgetter(1)))[0]

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

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

为了在python3中工作:

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

另外,如果你不介意使用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/


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

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()获得。


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')

你可以创建一个pandas Series,使用主列表作为数据,另一个列表作为索引,然后根据索引进行排序:

import pandas as pd
pd.Series(data=X,index=Y).sort_index().tolist()

输出:

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

一个简单的句子。

list_a = [5,4,3,2,1]
list_b = [1,1.5,1.75,2,3,3.5,3.75,4,5]

假设你想让列表a匹配列表b。

orderedList =  sorted(list_a, key=lambda x: list_b.index(x))

当需要将较小的列表与较大的列表进行排序时,这是很有用的。假设较大的列表包含较小列表中的所有值,就可以做到这一点。


list1 = ['a','b','c','d','e','f','g','h','i']
list2 = [0,1,1,0,1,2,2,0,1]

output=[]
cur_loclist = []

获取list2中的唯一值

list_set = set(list2)

查找list2中索引的loc

list_str = ''.join(str(s) for s in list2)

索引在list2中的位置使用cur_loclist跟踪

[0, 3, 7, 1, 2, 4, 8, 5, 6]

for i in list_set:
cur_loc = list_str.find(str(i))

while cur_loc >= 0:
    cur_loclist.append(cur_loc)
    cur_loc = list_str.find(str(i),cur_loc+1)

print(cur_loclist)

for i in range(0,len(cur_loclist)):
output.append(list1[cur_loclist[i]])
print(output)

下面是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是元组。 我也在想是否有更好的方法来做到这一点。

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


我创建了一个更通用的函数,它根据另一个列表对两个以上的列表进行排序,灵感来自@Whatang的答案。

def parallel_sort(*lists):
    """
    Sorts the given lists, based on the first one.
    :param lists: lists to be sorted

    :return: a tuple containing the sorted lists
    """

    # Create the initially empty lists to later store the sorted items
    sorted_lists = tuple([] for _ in range(len(lists)))

    # Unpack the lists, sort them, zip them and iterate over them
    for t in sorted(zip(*lists)):
        # list items are now sorted based on the first list
        for i, item in enumerate(t):    # for each item...
            sorted_lists[i].append(item)  # ...store it in the appropriate list

    return sorted_lists

实际上,我来这里是为了对值匹配的列表进行排序。

list_a = ['foo', 'bar', 'baz']
list_b = ['baz', 'bar', 'foo']
sorted(list_b, key=lambda x: list_a.index(x))
# ['foo', 'bar', 'baz']

这是一个老问题,但我看到的一些答案实际上并不能工作,因为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 ]

你可以用一行写出来:

X, Y = zip(*sorted(zip(Y, X)))

这个函数应该适用于数组。

def sortBoth(x,y,reverse=False):
    '''
    Sort both x and y, according to x. 
    '''
    xy_sorted=array(sorted(zip(x,y),reverse=reverse)).T
    return xy_sorted[0],xy_sorted[1]

我认为如果两个列表的大小不同或包含不同的项目,上面的大多数解决方案都是无效的。下面的解决方案很简单,应该可以解决这些问题:

import pandas as pd

list1 = ['B', 'A', 'C']  # Required sort order
list2 = ['C', 'A']       # Items to be sorted according to list1

result = pd.merge(pd.DataFrame(list1), pd.DataFrame(list2))
print(list(result[0]))

输出:

['A', 'C']

注意:任何不在list1中的项都将被忽略,因为算法不知道要使用的排序顺序。


上面的大多数解决方案都很复杂,我认为如果列表的长度不同或不包含完全相同的项目,它们就不会起作用。下面的解决方案很简单,不需要任何导入。

list1 = ['B', 'A', 'C']  # Required sort order
list2 = ['C', 'B']       # Items to be sorted according to list1

result = list1
for item in list1:
    if item not in list2: result.remove(item)

print(result)

输出:

['B', 'C']

注意:任何不在list1中的项都将被忽略,因为算法不知道要使用的排序顺序。


我认为原题的题目不准确。如果你有两个条目数量相同的列表,并且列表1中的每个条目都以相同的顺序与列表2相关(例如a = 0, b = 1,等等),那么问题应该是“如何对字典排序?”',而不是'如何根据另一个列表的值对列表进行排序?'。在这种情况下,下面的解决方案是最有效的:

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

dict1 = dict(zip(X,Y))
result = sorted(dict1, key=dict1.get)
print(result)

结果:

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