简体   繁体   English

Python 3.x — 根据另一个列表对一个列表进行排序,然后返回已排序的两个列表

[英]Python 3.x — sort one list based on another list and then return both lists sorted

Problem: I would like to sort one list based on another list and then return both lists sorted.问题:我想根据另一个列表对一个列表进行排序,然后返回已排序的两个列表。

I'm doing it like this:我这样做是这样的:

points = [4,7,3,2,7]
candidates = ['a', 'b', 'c', 'd', 'e']

ordered_candidates = [x for _, x in sorted(zip(points, candidates), reverse=True)]
ordered_points = sorted(points, reverse=True)

Question : Is it possible to merge the last two lines into one line?问题:是否可以将最后两行合并为一行?

I found question: Sorting list based on values from another list?我发现了问题: 根据另一个列表中的值对列表进行排序? but it's not exactly what I'm asking for.但这并不是我想要的。

Is it possible to merge the last two lines into one line?是否可以将最后两行合并为一行?

Yes it is.是的。 ordered_candidates excludes the points from the result, since you only choose the candidates with [x for _, x in...] . ordered_candidates从结果中排除分数,因为您只选择具有[x for _, x in...]候选人 This basically only selects the second item from each tuple.这基本上只从每个元组中选择第二项。 Additionally, ordered_points sorts only the points.此外, ordered_points仅对点进行排序。 They both also sort in reverse with reverse=True .它们也都使用reverse=True排序。

Seems like you can just modify ordered_candidates to include both items in the (point, candidate) pairs.似乎您可以修改ordered_candidates以将这两个项目都包含在(point, candidate)对中。

>>> points = [4,7,3,2,7]
>>> candidates = ['a', 'b', 'c', 'd', 'e']
>>> sorted(zip(points, candidates), reverse=True)
[(7, 'e'), (7, 'b'), (4, 'a'), (3, 'c'), (2, 'd')]

It is possible to do this by:可以通过以下方式做到这一点:

  • changing, slightly, what save in the inner list comprehension稍微改变内部列表理解中保存的内容
  • unpacking that list comprehension解包列表理解
  • zipping that unpacked data, which stores the results into two tuples the same length as the original inputs, but with the appropriate sorting preserved压缩解压后的数据,将结果存储到与原始输入长度相同的两个元组中,但保留了适当的排序
  • unpacking the results into two variables on a single line将结果解压缩为一行中的两个变量
points = [4,7,3,2,7]
candidates = ['a', 'b', 'c', 'd', 'e']

ordered_candidates, ordered_points = zip(*[(p, c) for p, c in sorted(zip(points, candidates), reverse=True)])```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基于另一个排序列表在python中对列表进行排序 - Sort a list in python based on another sorted list 基于另一个已排序的嵌套列表 python 对嵌套列表进行排序 - Sort nested list based on another sorted nested list python 根据另一个列表的排序方式对列表进行排序 - Sort list based on how another list is sorted 基于键排序列表的python排序列表 - python sort list based on key sorted list 获取 map() 以返回 Python 3.x 中的列表 - Getting a map() to return a list in Python 3.x 函数正在更改两个列表,而不是基于 Python 中的“0”的一个列表 - Function is changing both lists instead of one list based on '0' in Python 如何在特定条件下以特定顺序将列表放入一个更大的列表中? -Python 3.x - How to put lists into one larger list in a certain order for certain conditions? - Python 3.x 如何根据项目计数对降序排列的Python列表进行排序,但返回仅包含已排序项目的列表,而不返回计数? - How to sort a Python list descending based on item count, but return a list with sorted items only, and not a count also? 如何根据 2 个其他列表的排序方式对 1 个列表进行排序? - How to sort 1 list based on how 2 other lists were sorted? 在python中将排序的链表合并到一个排序的链表时出错 - Error in merging sorted linked lists to one sorted linked list in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM