简体   繁体   English

使用Python中的zip函数对2个相关列表进行排序,如何以降序排序?

[英]Sorting 2 lists that are related using zip function in Python, how to sort in descending order?

So, I am trying to sort 2 related lists. 因此,我正在尝试对2个相关列表进行排序。 One contains Candidate Names, and one contains the Number of Votes the candidates have (candidate in candidate[0] has votes stored in votes[0] ). 一个包含候选人名称,一个包含候选人拥有的投票数( candidate[0]中的candidate[0]票数存储在votes[0] )。

I found a way to sort votes[] in descending order, whilst keeping the indexes matched. 我找到了一种在保持索引匹配的同时对voice []进行降序排序的方法。 For example if vote[0] becomes vote[3] , candidate[0] will also become candidate[3] . 例如,如果vote[0]变为vote[3] ,则candidate[0]也将成为candidate[3] I did this using the built-in zip function, but the example I copied from sorted the lists in ascending order, whereas I require them to be sorted in descending order. 我使用内置的zip函数完成了此操作,但是我复制的示例是按升序对列表进行排序的,而我要求它们按降序进行排序。 Here are my lists: 这是我的清单:

candidates = ['Donald', 'Barack', 'Hillary', 'Mitt']
votes = [9, 7, 1, 3]

And to sort the lists I used: 并对我使用的列表进行排序:

votes, candidates = (list(t) for t in zip(*sorted(zip(votes, candidates))))

This did exactly what I wanted, except in ascending order, not descending. 这确实符合我的要求,只是按升序排列,不按降序排列。 How do I edit this to sort the lists into Descending order? 我该如何编辑以将列表按降序排序?

You could just sort the zip of both the list and reverse it 您可以只排序listzip并将其reverse

>>> votes = [9, 7, 1, 3]
>>> candidates = ['Donald', 'Barack', 'Hillary', 'Mitt']
>>> 
>>> sorted(zip(votes, candidates), key=lambda x: x[0]) # in ascending order
[(1, 'Hillary'), (3, 'Mitt'), (7, 'Barack'), (9, 'Donald')]
>>>
>>> sorted(zip(votes, candidates), key=lambda x: x[0], reverse=True) # in descending order
[(9, 'Donald'), (7, 'Barack'), (3, 'Mitt'), (1, 'Hillary')]

>>> # and if you want it back in order again;
>>> votes, names = zip(*sorted(zip(votes, candidates), key=lambda x: x[0], reverse=True))
>>> votes
(9, 7, 3, 1)
>>> names
('Donald', 'Barack', 'Mitt', 'Hillary')

using reverse=True : 使用reverse=True

candidates = ['Donald', 'Barack', 'Hillary', 'Mitt']
votes = [9, 7, 1, 3]

votes, candidates = (list(t) for t in zip(*sorted(zip(votes, candidates))))


print(sorted(votes, reverse=True))
print(sorted(candidates, reverse=True))

OUTPUT: OUTPUT:

[9, 7, 3, 1]
['Mitt', 'Hillary', 'Donald', 'Barack']

OR 要么

candidates = ['Donald', 'Barack', 'Hillary', 'Mitt']
votes = [9, 7, 1, 3]

print(sorted(zip(candidates, votes), reverse=True))

OUTPUT: OUTPUT:

[('Mitt', 3), ('Hillary', 1), ('Donald', 9), ('Barack', 7)]

EDIT 3: 编辑3:

candidates = ['Donald', 'Barack', 'Hillary', 'Mitt']
votes = [9, 7, 1, 3]

votes, candidates = (list(t) for t in zip(*sorted(zip(votes, candidates), reverse=True)))

print(votes)
print(candidates)

OUTPUT: OUTPUT:

[9, 7, 3, 1]
['Donald', 'Barack', 'Mitt', 'Hillary']

Or, slicing for reversing: 或者,切片可逆:

candidates = ['Donald', 'Barack', 'Hillary', 'Mitt']
votes = [9, 7, 1, 3]
votes, candidates = (list(t) for t in zip(*sorted(zip(votes, candidates))[::-1]))

暂无
暂无

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

相关问题 使用降序对列表列表进行排序,然后如果有平局,则对 python 中的另一个索引使用降序 - Sort a list of lists using descending order, and then if there is a tie breaker, use descending order on another index in python Python - 对 2 个列表进行排序 - 升序和降序并返回为 2 个列表 - Python - Sorting 2 Lists - both ascending and descending order and returning back as 2 lists 一个线性函数,用于根据python中的变化条件以升序,降序对列表的列表进行排序 - One liner function for sorting a string list of lists in ascending, descending order based on varying criteria in python 使用 python zip function 将两个列表一起排序 - Sorting two lists together in using python zip function 如何使用min和max函数按降序对值进行排序 - How to sort values in descending order using the min and max function 按降序排序python - Sort in descending order python 如何在python中按降序对数字进行排序? - how to sort numbers in a descending order in python? "如何按python降序对整数列表进行排序" - How to sort integer list in python descending order 如何在不使用排序 function 的情况下查找列表中的元素是升序还是降序? - How to find if the elements in a list are in ascending order or in descending order without using sort function? 如何使用 Python 中的排序算法对列表字典进行排序? - How to sort a dictionary of lists using a sorting algorithm in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM