来自python wiki:
In Py3.0, the cmp parameter was removed entirely (as part of a larger effort to simplify and unify the language, eliminating the conflict between rich comparisons and the __cmp__ methods).
我不明白为什么在py3.0中删除cmp的原因
考虑这个例子:
>>> def numeric_compare(x, y):
return x - y
>>> sorted([5, 2, 4, 1, 3], cmp=numeric_compare)
[1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)
现在考虑这个版本(推荐并与3.0兼容):
def cmp_to_key(mycmp):
'Convert a cmp= function into a key= function'
class K(object):
def __init__(self, obj, *args):
self.obj = obj
def __lt__(self, other):
return …Run Code Online (Sandbox Code Playgroud) 这似乎是一个重复的问题,但是我尝试了已经存在的解决方案,但到目前为止,对于我来说似乎没有任何解决方案。。
该解决方案给出了提示,但仅适用于常规几何体。我有一个相当复杂的几何图形,可以从中提取未排序的边界点。
如图所示,点的(x,y)坐标为:
import numpy as np
pts = np.array([[ 30. , -6.25 ],
[ 30. , -8.10127917],
[ 0. , -6.25 ],
[ 34.14082772, -6.75584268],
[ 36.49784598, -10. ],
[ 44.43561524, -10. ],
[ 100. , -10. ],
[ 100. , 10. ],
[ 84.1244615 , -10. ],
[ 84.1244615 , 10. ],
[ 36.49784598, 10. ],
[ 34.14082772, 6.75584268],
[ 44.43561524, 10. ],
[ 30. , 8.10127917],
[ 30. , 6.25 ],
[ 0. , 6.25 …Run Code Online (Sandbox Code Playgroud) 我有一个具有(x,y)格式坐标的元组列表。我想按逆时针方向对其进行排序/排列。例如:
[(0,1),(3,1),(-1,0),(2,2)]
Run Code Online (Sandbox Code Playgroud)
排列的清单应为:
[(3,1),(2,2),(0,1),(-1,0)]
Run Code Online (Sandbox Code Playgroud)
注意:列表中可以有“ n”个元组,并且(0,0)可以是列表的一部分。