如何以python降序对整数列表进行排序 [英] How to sort integer list in python descending order
本文介绍了如何以python降序对整数列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图以不同的方式解决这个问题,但没有成功.打印时,我一直保持升序排序,而不是降序.
I have tried to figure this out in different ways, to no success. I keep getting ascending order sort, rather than descending order when I print.
ListB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]
sorted(ListB, key=int, reverse=True)
print sorted(ListB)
推荐答案
您正在打印升序排列的列表:
You are printing the list sorted ascending:
print sorted(ListB)
如果您希望它降序,请将打印语句放在前一行(在此处反转)
If you want it descending, put your print statement on the previous line (where you reverse it)
print sorted(ListB, key=int, reverse=True)
然后删除您的最终打印声明.
Then remove your final print statement.
示例:
>>> ListB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]
>>> print sorted(ListB, key=int, reverse=True)
[48, 46, 25, 24, 22, 13, 8, -9, -15, -36]
这篇关于如何以python降序对整数列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文