Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:用“uuu cmp”uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;严格小于;_Python_Python 2.7_Sorting_Operators - Fatal编程技术网

Python:用“uuu cmp”uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;严格小于;

Python:用“uuu cmp”uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;严格小于;,python,python-2.7,sorting,operators,Python,Python 2.7,Sorting,Operators,我有一个库,它需要对间隔进行排序,并最终对点进行排序 我有一个\uuucmp\uuuu方法,该方法实现了非常稳定和逻辑的排序(代码见末尾)。而且,我有一个有用的\uu lt\uuu方法来查看间隔是否严格小于彼此(同上) 我有大约30个关于这种行为的测试,而且进展顺利 问题 …除非我必须排序: >>> sorted([Interval(0, 10), Interval(-10, 5)]) [Interval(0, 10), Interval(-10, 5)]

我有一个库,它需要对
间隔
进行排序,并最终对
进行排序

我有一个
\uuucmp\uuuu
方法,该方法实现了非常稳定和逻辑的排序(代码见末尾)。而且,我有一个有用的
\uu lt\uuu
方法来查看间隔是否严格小于彼此(同上)

我有大约30个关于这种行为的测试,而且进展顺利

问题 …除非我必须排序:

>>> sorted([Interval(0, 10), Interval(-10, 5)])
[Interval(0, 10), Interval(-10, 5)]               # WRONG!
由于Python使用
\uuult\uuuuu
,因此它使用了“严格小于”语义,而不是
\uuuuuucmp\uuuu
。我可以强制它显式地这样做,但我宁愿避免繁琐的语法:

>>> sorted([Interval(0, 10), Interval(-10, 5)], cmp=Interval.__cmp__)
[Interval(-10, 5), Interval(0, 10)]               # RIGHT
在Python3中,语法更加繁琐(Python2.6中不可用,Python2.7中可用)

我想要什么 有没有一种方法可以让
排序()
和朋友使用
间隔。\uuuu cmp\uuuu
自动,但仍然保持
\uu lt\uuuu
不变?也就是说,我想要这种优雅的行为:

>>> sorted([Interval(0, 10), Interval(-10, 5)])   # no extra arguments!
[Interval(-10, 5), Interval(0, 10)]
附录:《代码》和《代码》的实施
def\uuuu cmp\uuuu(自身、其他):
"""
说明其他排序是在此之前、之后还是等于此排序
间隔
排序是按开始,然后按结束,然后按数据字段。
如果数据字段不是两种可排序类型,则数据字段为
按类型名称的字母顺序进行比较。
:参数其他:间隔
:返回:-1、0、1
:rtype:int
"""
s=自我[0:2]
尝试:
o=其他[0:2]
除:
o=(其他,)
如果s!=o:
如果sreturn not self.overlaps(other)和self.end我强烈建议不要为类定义任何顺序运算符

原因是,顺序运算符的存在意味着存在一个顺序关系及其相关语义,而您的类本质上违反了它(在区间上没有定义良好的顺序关系)

例如,订单关系意味着:

not(a<b) and not(b<a) ==> a==b

获取所需内容的唯一方法是使用
inspect
模块进行堆栈检查。告诉是否由于排序后的比较而调用了
\uu lt\uuu
,如果是,则说明其行为不同。它会工作,但可能会很慢。哇。这是一个有趣的技巧。这就是其中的一个时刻:“我会出于行善的愿望而使用这枚戒指……但通过我,它将拥有一种无法想象的巨大而可怕的力量。”;)首先,谢谢你的回复!我想我需要一段时间才能理解这种哲学。同时,我添加了一个自定义的
sorted()
方法。由于Python3不支持
sorted(…,cmp=…)
而只支持
sorted(…,key=…)
我必须创建一个自定义
key()
方法。
def __cmp__(self, other):
    """
    Tells whether other sorts before, after or equal to this
    Interval.

    Sorting is by begins, then by ends, then by data fields.

    If data fields are not both sortable types, data fields are
    compared alphabetically by type name.
    :param other: Interval
    :return: -1, 0, 1
    :rtype: int
    """
    s = self[0:2]
    try:
        o = other[0:2]
    except:
        o = (other,)
    if s != o:
        return -1 if s < o else 1
    try:
        if self.data == other.data:
            return 0
        return -1 if self.data < other.data else 1
    except TypeError:
        s = type(self.data).__name__
        o = type(other.data).__name__
        if s == o:
            return 0
        return -1 if s < o else 1

def __lt__(self, other):
    """
    Less than operator. Returns False if there is an overlap.
    :param other: Interval or point
    :return: True or False
    :rtype: bool
    """
    return not self.overlaps(other) and self.end <= other
not(a<b) and not(b<a) ==> a==b
def sort_intervals(lst):
    return sorted(lst, cmp = ...)