在python sorted()中反转第二排序特征 [英] reverse second sort characteristic in python sorted()

查看:143
本文介绍了在python sorted()中反转第二排序特征的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对列表进行排序,但我想不出要颠倒第二个排序特征的顺序.

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),5:(-2,-2),6:(3,3),7:(1,-1)} 
l = []
for x in range(1,8):

    l.append((math.atan2(ps[x][1],ps[x][0]),ps[x]))

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

第一个分类特征是按角度分类,如果角度相等,则第二个分类特征是按距原点的距离分类.有谁知道如何做到这一点.在此先感谢您的帮助.

The first sort characteristic is sorting by angle, the second sorts by distance from the origin if the angle is equal. Does anyone know how to do this. Thanks in advance for the help.

推荐答案

在第二个排序特征前面放置一个减号:

Put a minus sign in front of the second sort characteristic:

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

这篇关于在python sorted()中反转第二排序特征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆