欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python3实现自定义比较排序/运算符

程序员文章站 2022-03-02 14:52:55
目录自定义比较排序/运算符1.cmp函数2.重写类方法python3实现各种排序方法自定义比较排序/运算符python3和python2相比有挺多变化。在python2中可以直接写一个cmp函数作为参...

自定义比较排序/运算符

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

python3实现各种排序方法

# coding=gbk
import random
from array import array
def swap(lyst,i,j):
    temp = lyst[i]
    lyst[i] = lyst[j]
    lyst[j] = temp
#选择排序,复杂度o(n^2)
def selectionsort(lyst): 
    i = 0
    while i < len(lyst) - 1:
        minindex = i
        j = i + 1
        while j < len(lyst):
            if lyst[j] < lyst[minindex]:
                minindex = j
            j += 1
        if minindex != i:
            swap(lyst,minindex,i)
        i += 1
#冒泡排序,复杂的o(n^2)
def bubblesort(lyst):
    n = len(lyst)
    while n > 1:
        i = 1
        while i < n:
            if lyst[i] < lyst[i-1]:
                swap(lyst,i,i-1)
            i += 1
        n -= 1
#冒泡排序优化改进最好情况
def bubblesortwithtweak(lyst):
    n = len(lyst)
    while n > 1:
        swapped = false
        i = 1
        while i < n:
            if lyst[i] < lyst[i-1]:
                swap(lyst,i,i-1)
                swapped = true
            i += 1
        if not swapped: return
        n -= 1
#插入排序,复杂的o(n^2)
def insertionsort(lyst):
    i = 1
    while i < len(lyst):
        itemtoinsert = lyst[i]
        j = i - 1
        while j >= 0:
            if itemtoinsert < lyst[j]:
                lyst[j+1] = lyst[j]
                j -= 1
            else:
                break
        lyst[j+1] = itemtoinsert
        i += 1
#快速排序,最好情况,复杂的o(n*(log2 n)),最坏情况,复杂的o(n^2)
def quicksort(lyst):
    quicksorthelper(lyst,0,len(lyst)-1)
def quicksorthelper(lyst,left,right):
    if left < right:
        pivotlocation = partition(lyst,left,right)
        quicksorthelper(lyst,left,pivotlocation-1)
        quicksorthelper(lyst,pivotlocation+1,right)
def partition(lyst,left,right):
    middle = (left+right) // 2
    pivot = lyst[middle]
    lyst[middle] = lyst[right]
    lyst[right] = pivot
    boundary = left
    for index in range(left,right):
        if lyst[index] < pivot:
            swap(lyst,index,boundary)
            boundary += 1
    swap(lyst,right,boundary)
    return boundary
#合并排序
def mergesort(lyst):
    copybuffer = [0]*(len(lyst))
    mergesorthelper(lyst,copybuffer,0,len(lyst)-1)
def mergesorthelper(lyst,copybuffer,low,high):
    if low < high:
        middle = (low+high)//2
        mergesorthelper(lyst,copybuffer,low,middle)
        mergesorthelper(lyst,copybuffer,middle+1,high)
        merge(lyst,copybuffer,low,middle,high)
def merge(lyst,copybuffer,low,middle,high):
    i1 = low
    i2 = middle + 1
    for i in range(low,high+1):
        if i1 > middle:
            copybuffer[i] = lyst[i2]
            i2 += 1
        elif i2 > high:
            copybuffer[i] = lyst[i1]
            i1 += 1      
        elif lyst[i1] < lyst[i2]:
            copybuffer[i] = lyst[i1]
            i1 += 1      
        else :
            copybuffer[i] = lyst[i2]
            i2 += 1 
    for i in range(low,high+1):
        lyst[i] = copybuffer[i]
def main(size = 20,sort = mergesort):   
    lyst = []
    for count in range(size):
        lyst.append(random.randint(1,size+1))
    print(lyst)
    sort(lyst)
    print(lyst)
if __name__ == "__main__":
    main()

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。