Python 基于其他列表中的值对列表进行排序
我有一个字符串列表,如下所示:Python 基于其他列表中的值对列表进行排序,python,list,sorting,Python,List,Sorting,我有一个字符串列表,如下所示: 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"] 具有相同“键”的元素的顺序并不重要。对于构造,我可以求助于使用,但我很好奇是否有较短的方法。有什么建议吗?最短代码
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"]
具有相同“键”的元素的顺序并不重要。对于构造,我可以求助于使用
,但我很好奇是否有较短的方法。有什么建议吗?最短代码
[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])]
解释:
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"]
两个列表
s
使用基于zip
创建新的排序列表
使用列表理解从已排序、压缩的列表
中提取每对的第一个元素
有关如何设置\通常使用键
参数和排序
功能的更多信息,请参阅
对我来说,最明显的解决方案是使用键
关键字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)
正如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']
由于所描述的装饰排序非装饰方法稍微简单一些,并且在所有情况下都有效,因此在大多数情况下可能更好。(这是一个非常古老的答案!)将两个列表压缩在一起,进行排序,然后选择所需的部分:
>>> 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]
我在这里找到的:
我喜欢有一个排序索引列表。这样,我可以按照与源列表相同的顺序对任何列表进行排序。一旦您有了一个排序索引列表,简单的列表理解就可以做到这一点:
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()
获取排序索引列表。argsort()有一个用于并行排序iterables的工具:
给定的
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')
您可以创建一个熊猫系列
,使用主列表作为数据
,使用另一个列表作为索引
,然后仅按索引排序:
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 = []
获取列表2中存在的唯一值
list_set = set(list2)
在列表2中查找索引的loc
list_str = ''.join(str(s) for s in list2)
使用cur\u 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)
如果您想获得两个排序列表(python3),下面是您的答案
记住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
不可编写脚本。其他答案不必费心于导入操作员
,而是在此处提供有关此模块及其优点的更多信息
对于这个问题,至少有两个好的习惯用法。从您提供的示例输入开始:
X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
Y = [ 0, 1, 1, 0, 1, 2, 2, 0, 1 ]
使用“”成语
这也被称为90年代在Perl中推广此模式的after:
# 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
),依此类推。这可以创建输出,除非您包含字典顺序的原始列表索引,以保持副本的原始顺序
使用
这使您可以更直接地控制如何对输入进行排序,因此只需声明要排序的特定键即可获得。请参阅更多示例
您可以在一行中执行此操作:
X, Y = zip(*sorted(zip(Y, X)))
这是正确的,但我要补充一点,如果您试图按同一个数组对多个数组进行排序,则这不一定会按预期工作,因为用于排序的键是(y,x),而不仅仅是y。您应该改为在排序(zip(y,x),key=lambda-pair:pair[0])中使用[x代表(y,x)]!但应该是这样的:列表是按照对中的第一个元素排序的,理解提取对中的“第二个”元素。这个解决方案在存储方面很差。只要可能,最好使用就地排序。@Hatefind有趣的是,您能指出如何实现这一点的参考吗?@RichieV我建议使用快速排序或就地合并排序实现。一旦有了这个功能,就可以定义自己的比较函数,该函数根据listY
的索引来比较值。最终结果应该是listY
保持不变,并且listX
在不必创建临时列表的情况下被更改为预期的解决方案。riza的答案在绘制数据时可能很有用,因为zip(*排序(zip(X,Y),key=lambda pair:pair[0])返回排序后的X和Y,并使用X值进行排序。如果X
是str
的列表,则可以这样做,但如果在尝试对zip对象使用排序时,AttributeError:“zip”对象没有属性“sort”
是我现在得到的,请小心。您正在使用Python 3。在Python2中,zip生成了一个列表。现在它生成了一个iterable对象<代码>排序(zip()。
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)))