1. 程式人生 > >Python3 自定義比較運算子/排序

Python3 自定義比較運算子/排序

Python3和Python2相比有挺多變化。

在Python2中可以直接寫一個cmp函式作為引數傳入sort來自定義排序,但是Python3取消了。

在這裡總結一下Python3的自定義排序的兩種寫法,歡迎補充。

我們以二維空間中的點來作為待排序的資料結構,我們希望能先比較x後再比較y。

class Pos:
    def __init__(self, x = 0, y = 0):
        self.x = x
        self.y = y

    def __str__(self):
        return ('(%s, %s)' % (self.x, self.y))

    __repr__ = __str__

1.cmp函式

第一種方法我們還是以重寫cmp或lambda表示式的形式,和Python2很類似

注意,此方法用sorted是不能成功排序的

只是要藉助functools

import functools
def cmp(a, b):
    return a.x-b.x if a.x != b.x else a.y-b.y  # x y均按照從小到大的順序

if __name__ == '__main__':

    test_list = [Pos(5, 1), Pos(2,5), Pos(2, 4)]
    # test_list.sort(key=functools.cmp_to_key(lambda a,b: a.x-b.x if a.x != b.x else a.y-b.y))
    test_list.sort(key=functools.cmp_to_key(cmp))
    # sorted(test_list, key=functools.cmp_to_key(cmp))  #    親測此方法不能成功排序
    print(test_list)  # 輸出結果 [(2, 4), (2, 5), (5, 1)]

2.重寫類方法

Python2中可以直接重寫__cmp__方法來實現比較,但是Python3中已經取消了.

Python3中需要細分每一個比較運算子.

__lt__: <
__gt__: >
__ge__: >=
__eq__: ==
__le__: <=

實現如下

class Pos:
    def __init__(self, x = 0, y = 0):
        self.x = x
        self.y = y

    def __str__(self):
        return ('(%s, %s)' % (self.x, self.y))

    def __lt__(self, other):
        print('lt: ' + str(self))
        return self.x < other.x if self.x != other.x else self.y < other.y

    def __gt__(self, other):
        print('gt: ' + str(self))
        return self.x > other.x if self.x != other.x else self.y > other.y

    def __ge__(self, other):
        print('ge: ' + str(self))
        return self.x >= other.x if self.x != other.x else self.y >= other.y

    def __eq__(self, other):
        print('eq: ' + str(self))
        return self.x == other.x and self.y == other.y

    def __le__(self, other):
        print('le: ' + str(self))
        return self.x <= other.x if self.x != other.x else self.y <= other.y

    __repr__ = __str__

我們實踐一下

if __name__ == '__main__':

    if Pos(5,1) <= Pos(2,4):
        print('True!')
    if Pos(5,1) == Pos(2,4):
        print('True!')
    if Pos(5,1) > Pos(2,4):
        print('True!')
# 輸出
# le: (5, 1)
# eq: (5, 1)
# gt: (5, 1)
# True!

最後我們回到排序

if __name__ == '__main__':

    test_list = [Pos(5, 1), Pos(2,5), Pos(2, 4)]
    test_list.sort()
    print(test_list)

    test_list.sort(reverse=True)
    print(test_list)

# 輸出
# lt: (2, 5)
# lt: (2, 4)
# [(2, 4), (2, 5), (5, 1)]
# lt: (2, 5)
# lt: (2, 4)
# [(5, 1), (2, 5), (2, 4)]