我有一个字符串列表,像这样:
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结构,但我很好奇是否有更短的方法。有什么建议吗?
当前回答
我创建了一个更通用的函数,它根据另一个列表对两个以上的列表进行排序,灵感来自@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
其他回答
Zip,按第二列排序,返回第一列。
zip(*sorted(zip(X,Y), key=operator.itemgetter(1)))[0]
对我来说,最明显的解决方案是使用关键字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所描述的装饰-排序-不装饰方法稍微简单一些,并且适用于所有情况,因此在大多数情况下它可能更好。(这是一个非常古老的答案!)
我认为原题的题目不准确。如果你有两个条目数量相同的列表,并且列表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']
我认为如果两个列表的大小不同或包含不同的项目,上面的大多数解决方案都是无效的。下面的解决方案很简单,应该可以解决这些问题:
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]))
输出:
['A', 'C']
注意:任何不在list1中的项都将被忽略,因为算法不知道要使用的排序顺序。
上面的大多数解决方案都很复杂,我认为如果列表的长度不同或不包含完全相同的项目,它们就不会起作用。下面的解决方案很简单,不需要任何导入。
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)
输出:
['B', 'C']
注意:任何不在list1中的项都将被忽略,因为算法不知道要使用的排序顺序。