在python中对列表进行排序 [英] Sort a list in python
本文介绍了在python中对列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个清单
[1,-5,10,6,3,-4,-9]
但是现在我希望列表按以下顺序排序:
But now I want the list to be sorted like this:
[10,-9,6,-5,-4,3,1]
如您所见,无论每个数字具有什么符号,我都希望从高到低排序,但是保持符号清晰吗?
As you can see I want to order from high to low no matter what sign each number has, but keeping the sign, is it clear?
推荐答案
使用abs
作为sorted
函数或list.sort
的键:
>>> lis = [1,-5,10,6,3,-4,-9]
>>> sorted(lis, key=abs, reverse=True)
[10, -9, 6, -5, -4, 3, 1]
这篇关于在python中对列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文