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)]

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值