我有一个字符串列表,像这样:
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 ]
使用Y中的值对X进行排序以得到以下输出的最短方法是什么?
["a", "d", "h", "b", "c", "e", "i", "f", "g"]
具有相同“键”的元素的顺序并不重要。我可以使用for结构,但我很好奇是否有更短的方法。有什么建议吗?
当前回答
一个简单的句子。
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))
当需要将较小的列表与较大的列表进行排序时,这是很有用的。假设较大的列表包含较小列表中的所有值,就可以做到这一点。
其他回答
最短的代码
[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所描述的装饰-排序-不装饰方法稍微简单一些,并且适用于所有情况,因此在大多数情况下它可能更好。(这是一个非常古老的答案!)
这是一个老问题,但我看到的一些答案实际上并不能工作,因为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')
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是元组。 我也在想是否有更好的方法来做到这一点。
警告:如果你用空列表运行它,它会崩溃。