reverse second sort characteristic in python sorted()

前端 未结 4 704
闹比i
闹比i 2021-01-21 03:15

I\'m trying to sort a list and I can\'t figure out to reverse the order of a second sorting characteristic.

import math
ps = {1:(1,1),2:(3,2),3:(3,-3),4:(-3,4),         


        
4条回答
  •  死守一世寂寞
    2021-01-21 04:13

    You can do two sorts, least significant first. Python sorting is stable, so the order determined by the first sort will hold when you do the second.

    for c in sorted(sorted(l, key = lambda t:math.sqrt((0-t[1][0])**2 + (0-t[1][1])**2)), key = lambda t:t[0], reverse=True):
    

    This method works even when the keys aren't numeric.

提交回复
热议问题