data = [ (2,2), (0,5), (8,0), (9,8), (7,14), (13,12), (14,13) ]
我希望这个元组列表按如下方式排序:
data = [ (14,13), (7,14), (13,12), (9,8), (8,0), (0,5), (2,2) ]
顺序如下:
[ [(coordinates with largest X)],[(coordinates with largest Y)], [(coordinates with second largest X)], [(coordinates with second largest Y)], [(Third Largest X)], [(Third Largest y)] ... and so on ]
我试着用
data.sort(key=lambda x:x[0],reverse=True)
首先,然后
data.sort(key=lambda x:x[1],reverse=True)
但它不起作用。你知道吗
你觉得我该怎么办? 请帮忙
下面是一个使用Numpy的解决方案:
结果:
据我所知,sort函数不能满足您的需要。排序算法假定您没有更改关于如何在每个其他位置比较元素的规则。你知道吗
这里有一个O(N^2)算法来做你想做的事情:
试试这个:
相关问题 更多 >
编程相关推荐