[英]Sorting list based on values from another list
我有一个这样的字符串列表:
X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
Y = [ 0, 1, 1, 0, 1, 2, 2, 0, 1 ]
使用 Y 中的值对 X 进行排序以获得以下 output 的最短方法是什么?
["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])]
解释:
有关如何设置\使用key
参数以及一般sorted
的 function 的更多信息,请查看此。
Zip 将两个列表放在一起,排序,然后取出你想要的部分:
>>> 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))]
Also, if you don't mind using numpy arrays (or in fact already are dealing with numpy arrays...), here is another nice solution:
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/
对我来说最明显的解决方案是使用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描述的 decorate-sort-undecorate 方法稍微简单一些,并且适用于所有情况,因此大多数情况下它可能会更好。 (这是一个非常古老的答案!)
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]
演示
sort_together([Y, X])[1]
# ('a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g')
实际上,我来这里是为了按值匹配的列表对列表进行排序。
list_a = ['foo', 'bar', 'baz']
list_b = ['baz', 'bar', 'foo']
sorted(list_b, key=lambda x: list_a.index(x))
# ['foo', 'bar', 'baz']
我喜欢有一个排序索引列表。 这样,我可以按照与源列表相同的顺序对任何列表进行排序。 一旦你有了一个排序索引列表,一个简单的列表理解就可以了:
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]
zip,按第二列排序,返回第一列。
zip(*sorted(zip(X,Y), key=operator.itemgetter(1)))[0]
这是一个老问题,但我看到的一些答案实际上并不起作用,因为zip
不可编写脚本。 其他答案并没有打扰import operator
并在此处提供有关此模块及其好处的更多信息。
对于这个问题,至少有两个好的习语。 从您提供的示例输入开始:
X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
Y = [ 0, 1, 1, 0, 1, 2, 2, 0, 1 ]
这也称为R之后的Schwartzian_transform 。 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')
一个快速的单线。
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))
当需要将较小的列表排序为较大的值时,这很有用。 假设较大的列表包含较小列表中的所有值,则可以这样做。
我创建了一个更通用的 function,它根据@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
您可以创建一个pandas Series
,使用主列表作为data
,另一个列表作为index
,然后按索引排序:
import pandas as pd
pd.Series(data=X,index=Y).sort_index().tolist()
output:
['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g']
如果您想获得两个排序列表(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 是元组。 如果有更好的方法可以做到这一点,我也在徘徊。
警告:如果您使用空列表运行它,它会崩溃。
这个 function 应该适用于 arrays。
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]
我认为如果 2 个列表的大小不同或包含不同的项目,上面的大多数解决方案都将不起作用。 下面的解决方案很简单,应该可以解决这些问题:
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]))
output:
['A', 'C']
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
中查找索引的位置
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)
您可以获取一个排序列表的索引并将其应用于另一个,例如
idx = sorted(range(len(Y)), key=lambda i: Y[i])
ans = [X[i] for i in idx]
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)))
上面的大多数解决方案都很复杂,我认为如果列表长度不同或不包含完全相同的项目,它们将不起作用。 下面的解决方案很简单,不需要任何导入。
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)
Output:
['B', 'C']
对于 java,我使用选择排序得到的是:-
static void Sorting_list_based_on_values_from_another_list(){
String [] X = {"a", "b", "c", "d", "e", "f", "g", "h", "i" };
int [] Y = { 0, 1, 1, 0, 1, 2, 2, 0, 1 };
for(int i=0;i<=Y.length;i++){
for (int j=i;j<Y.length;j++){
if(Y[i]>Y[j]){
int n=Y[i];
Y[i] = Y[j];
Y[j] = n;
// swpping alphabets
String x=X[i];
X[i]=X[j];
X[j]=x;
}
}
}
for (int a:Y) {
System.out.print(a +" ");
}
System.out.println("");
for (String a:X) {
System.out.print(a+" ");
}
}
我认为原始问题的标题不准确。 如果您有 2 个包含相同数量项目的列表,并且列表 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']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.