【发布时间】:2018-03-16 18:25:46
【问题描述】:
我想按点到原点 (0,0) 的距离以升序对 2D 坐标系中的点进行排序。我找到了this 并尝试了一些方法,但仍然无法获得想要的结果。
这是我的代码:
from functools import cmp_to_key
def my_comp(point1, point2):
return point1[0]*point1[0] + point1[1]*point1[1] < point2[0]*point2[0] + point2[1]*point2[1]
points = [ [3.1, 4.1], [0.9, 0.8], [1.0, 1.0] ]
sorted(points, key=cmp_to_key(my_comp))
print(points)
结果:
[[3.1, 4.1], [0.9, 0.8], [1.0, 1.0]]
预期:
[[0.9, 0.8], [1.0, 1.0], [3.1, 4.1]]
【问题讨论】:
-
你得到了什么结果,为什么不符合你的预期?
-
result 是作为初始数组,想要的看起来像这样
[[0.9, 0.8], [1.0, 1.0], [3.1, 4.1]] -
为了完整起见,您应该将其放在您的问题中。
-
对不起,我的错。固定
标签: python python-3.x