Sort a list in python

后端 未结 3 889
执念已碎
执念已碎 2020-12-03 11:30

I have this list

[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]

3条回答
  •  难免孤独
    2020-12-03 11:51

    Use abs as key to the sorted function or list.sort:

    >>> lis = [1,-5,10,6,3,-4,-9]
    >>> sorted(lis, key=abs, reverse=True)
    [10, -9, 6, -5, -4, 3, 1]
    

提交回复
热议问题