相关疑难解决方法(0)

为什么在Python3.0中从排序/排序中删除了cmp参数?

来自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)

python sorting object cmp

36
推荐指数
2
解决办法
1万
查看次数

顺时针/逆时针排序复杂的二维欧几里得点集合以形成闭合环

这似乎是一个重复的问题,但是我尝试了已经存在的解决方案,但到目前为止,对于我来说似乎没有任何解决方案。。

解决方案给出了提示,但仅适用于常规几何体。我有一个相当复杂的几何图形,可以从中提取未排序的边界点。

下面是我从几何图形中提取的几何图形和边界顶点的图片。 geometry_triangulation 在此处输入图片说明

如图所示,点的(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)

python numpy computational-geometry

6
推荐指数
1
解决办法
105
查看次数

如何对逆时针方向的元组列表进行排序?

我有一个具有(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)可以是列表的一部分。

python

4
推荐指数
1
解决办法
94
查看次数

标签 统计

python ×3

cmp ×1

computational-geometry ×1

numpy ×1

object ×1

sorting ×1