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

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 = ["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)))

其他回答

最短的代码

[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参数以及一般的排序函数的更多信息,请参阅以下内容。


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

我创建了一个更通用的函数,它根据另一个列表对两个以上的列表进行排序,灵感来自@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

我认为原题的题目不准确。如果你有两个条目数量相同的列表,并且列表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']

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

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