根据并行列表中的相应值对列表进行排序[重复]

2024-11-22 08:47:00
admin
原创
221
摘要:问题描述:我有一个如下字符串列表:X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"...

问题描述:

我有一个如下字符串列表:

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构造,但我想知道是否有更短的方法。有什么建议吗?


解决方案 1:

最短代码

[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])]

解释:

  1. zip两个lists。

  2. 创建一个新的,list根据zip使用进行排序sorted()

  3. 使用列表推导从已排序的压缩文件中提取每对的第一个元素list

有关如何设置\使用key参数以及sorted常规功能的更多信息,请查看此处。


解决方案 2:

将两个列表压缩在一起,进行排序,然后取出所需的部分:

>>> 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))]

解决方案 3:

此外,如果您不介意使用 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/

解决方案 4:

对我来说最明显的解决方案是使用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描述的装饰-排序-去装饰方法更简单一些,并且在所有情况下都有效,因此在大多数情况下它可能更好。 (这是一个非常古老的答案!)

解决方案 5:

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')

解决方案 6:

我实际上来这里是想根据值匹配的列表对列表进行排序。

list_a = ['foo', 'bar', 'baz']
list_b = ['baz', 'bar', 'foo']
sorted(list_b, key=lambda x: list_a.index(x))
# ['foo', 'bar', 'baz']

解决方案 7:

另一种选择是,结合几种答案。

zip(*sorted(zip(Y,X)))[1]

为了适用于 python3:

list(zip(*sorted(zip(B,A))))[1]

解决方案 8:

我喜欢有一个排序索引列表。这样,我可以按照与源列表相同的顺序对任何列表进行排序。一旦你有一个排序索引列表,一个简单的列表理解就可以解决问题:

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()

解决方案 9:

zip,按第二列排序,返回第一列。

zip(*sorted(zip(X,Y), key=operator.itemgetter(1)))[0]

解决方案 10:

这是一个老问题,但我看到的一些答案实际上不起作用,因为zip它不可编写脚本。其他答案没有费心import operator,在这里提供了有关此模块及其好处的更多信息。

这个问题至少有两个好的习惯用法。从您提供的示例输入开始:

X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
Y = [ 0,   1,   1,   0,   1,   2,   2,   0,   1 ]

使用“装饰-排序-取消装饰”习语

这也被称为 Schwartzian_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')

解决方案 11:

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)))

解决方案 12:

您可以创建一个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']

解决方案 13:

如果您想获得两个排序列表(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 是元组。我也想知道是否有更好的方法可以做到这一点。

警告:如果使用空列表运行它,它会崩溃。

解决方案 14:

受到@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

解决方案 15:

上面的大多数解决方案都很复杂,我认为如果列表长度不同或不包含完全相同的项目,它们将不起作用。下面的解决方案很简单,不需要任何导入。

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 中的项目都将被忽略,因为算法不知道要使用的排序顺序。

解决方案 16:

快速的一行。

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))

当需要将较小列表排序为较大列表中的值时,这很有用。假设较大列表包含较小列表中的所有值,则可以完成此操作。

解决方案 17:

该函数应该适用于数组。

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]

解决方案 18:

我认为如果两个列表的大小不同或包含不同的项目,上述大多数解决方案都将不起作用。下面的解决方案很简单,应该可以解决这些问题:

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 中的项目都将被忽略,因为算法不知道要使用的排序顺序。

解决方案 19:

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)

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)

解决方案 20:

我认为原始问题的标题不准确。如果您有 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']
相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   3958  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   2737  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Freshdesk、ClickUp、nTask、Hubstaff、Plutio、Productive、Targa、Bonsai、Wrike。在当今快速变化的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多企业在项目管理过程中面临着诸多痛点,如任务分配不...
项目管理系统   74  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Monday、TeamGantt、Filestage、Chanty、Visor、Smartsheet、Productive、Quire、Planview。在当今快速变化的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多项目经理和团队在管理复杂项目时,常...
开源项目管理工具   83  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Smartsheet、GanttPRO、Backlog、Visor、ResourceGuru、Productive、Xebrio、Hive、Quire。在当今快节奏的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多企业在选择项目管理工具时常常面临困惑:...
项目管理系统   70  
热门文章
项目管理软件有哪些?
曾咪二维码

扫码咨询,免费领取项目管理大礼包!

云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用