简体   繁体   English

以降序对列表进行排序,如果数字相同,则在 Python 中按字母顺序升序排序

[英]Sorting a list in Descending numerical order and if number is the same, sort by Ascending alphabetical in Python

I have 2 lists where I have to sort them both by descending order in the second list.我有 2 个列表,我必须在第二个列表中按降序对它们进行排序。

lst1 = ['Chris','Amanda','Boris','Charlie']
lst2 = [35,43,55,35]

I have sorted them by using我已经通过使用对它们进行了排序

lst2, lst1 = (list(t) for t in zip(*sorted(zip(lst2, lst1), reverse=True)))

Because they are sorted using reverse=True my result is sorted by descending alphabetical order as well因为它们是使用reverse=True排序的,所以我的结果也按字母顺序降序排序

This produces the result ['Boris', 'Amanda', 'Chris', 'Charlie'], [55, 43, 35, 35]这会产生结果['Boris', 'Amanda', 'Chris', 'Charlie'], [55, 43, 35, 35]

is there a way to produce the result ['Boris', 'Amanda', 'Charlie', 'Chris'], [55, 43, 35, 35] ?有没有办法产生结果['Boris', 'Amanda', 'Charlie', 'Chris'], [55, 43, 35, 35]

Try this:尝试这个:

lst1 = ['Chris','Amanda','Boris','Charlie']
lst2 = [35,43,55,35]

lst1, lst2 = zip(*sorted(zip(lst1, lst2), key=lambda p: (-p[1], p[0])))

This produces:这产生:

>>> lst1
['Boris', 'Amanda', 'Charlie', 'Chris']
>>> lst2
[55, 43, 35, 35]
>>> 

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

相关问题 从 Python 中的字母数字字符串列表执行降序数字,然后按字母顺序升序排序 - Perform a descending numerical and then ascending alphabetical sort from a list of alphanumeric strings in Python 我不能使用sorted函数对两个元素元组列表按降序排列,然后在出现领带的情况下按字母升序排序 - I can't use the sorted function to sort a two elements tuple list in descending numerical order then in ascending alphabetical order in case of a tie 列表排序(升序和降序) - List Sorting (ascending and descending order) 按升序和降序对列表进行排序 - Sort list in ascending and descending order 如何先按数字降序排序,然后按字母升序排序 - How to sort a list by number first in descending order and then by alphabet in ascending order 在django中按升序/降序对筛选列表进行排序 - Sorting a filtered list in ascending/descending order in django 使用字符串按升序和降序对列表列表进行排序 - sort list of lists in ascending and descending order with strings 如何按数字顺序而不是字母顺序对列表中的元素进行排序 - how to sort the elements in a list in numerical order and not alphabetical 冒泡排序帮助python - 升序和降序 - Bubble Sort help in python - ascending & descending order Python:按升序和降序合并排序 - Python: Merge Sort in ascending and descending order
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM