python - 根据另一个列表中的值排序列表?
我有一个像这样的字符串列表:
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构造,但我很好奇,如果有一个较短的方法。 有什么建议?
Legend asked 2019-02-23T06:27:13Z
14个解决方案
310 votes
最短的代码
[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])]
解释:
key两个keys。
使用key创建一个新的,分类key,基于sorted。
使用列表解析从排序的,压缩的key中提取每对的第一个元素。
有关如何设置\使用key参数以及sorted函数的更多信息,请查看此内容。
Whatang answered 2019-02-23T06:29:48Z
91 votes
将两个列表压缩在一起,对其进行排序,然后获取所需的部分:
>>> 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))]
Ned Batchelder answered 2019-02-23T06:30:41Z
53 votes
另外,如果你不介意使用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/]
Tom answered 2019-02-23T06:32:21Z
29 votes
对我来说最明显的解决方案是使用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)
senderle answered 2019-02-23T06:35:48Z
11 votes
我喜欢有一个排序索引列表。 这样,我可以按照与源列表相同的顺序对任何列表进行排序。 一旦你有了一个排序索引列表,一个简单的列表理解就可以了:
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()获取已排序的索引列表。
1-ijk answered 2019-02-23T06:36:31Z
7 votes
另一种选择,结合了几个答案。
zip(*sorted(zip(Y,X)))[1]
为了工作python3:
list(zip(*sorted(zip(B,A))))[1]
TMC answered 2019-02-23T06:37:04Z
4 votes
zip,按第二列排序,返回第一列。
zip(*sorted(zip(X,Y), key=operator.itemgetter(1)))[0]
riza answered 2019-02-23T06:37:29Z
4 votes
more_itertools有一个并行排序iterables的工具:
from more_itertools import sort_together
sort_together([Y, X])[1]
# ('a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g')
pylang answered 2019-02-23T06:37:54Z
1 votes
一个快速的单行。
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))
当需要将较小的列表排序为较大的值时,这很有用。 假设较大的列表包含较小列表中的所有值,则可以完成。
Evan Lalo answered 2019-02-23T06:38:34Z
1 votes
我创建了一个更通用的功能,根据@ 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
pgmank answered 2019-02-23T06:39:02Z
1 votes
我实际上是来这里寻找按列表匹配的列表。
list_a = ['foo', 'bar', 'baz']
list_b = ['baz', 'bar', 'foo']
sorted(list_b, key=lambda x: list_a.index(x))
# ['foo', 'bar', 'baz']
nackjicholson answered 2019-02-23T06:39:28Z
0 votes
您可以创建一个pandas Series,使用主列表data,另一个列表index,然后按索引排序:
import pandas as pd
pd.Series(data=X,index=Y).sort_index().tolist()
输出:
['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g']
Binyamin Even answered 2019-02-23T06:39:56Z
0 votes
如果你想获得两个排序列表(python3),这里是Whatangs的答案。
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是元组。如果有更好的方法,我也会徘徊。
警告:如果使用空列表运行它会崩溃。
Anoroah answered 2019-02-23T06:40:38Z
0 votes
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)
使用cur_loclist跟踪list2中的索引位置
[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)
VANI answered 2019-02-23T06:41:29Z