Python 3.x 对于预期的输出python3.x,Total ordering不返回

Python 3.x 对于预期的输出python3.x,Total ordering不返回,python-3.x,sorting,Python 3.x,Sorting,我有一个包含6个人的输入,每个人都有名字和号码。 我正在尝试编写排序函数,根据它们的编号对它们进行排序。如果两个数字是相同的,我会根据名字对它们进行排序 为了实现它,我使用total_排序。但是,它不会返回预期的输出 import numpy as np from functools import total_ordering @total_ordering class Person(object): def __init__(self, name, number):

我有一个包含6个人的输入,每个人都有名字和号码。 我正在尝试编写排序函数,根据它们的编号对它们进行排序。如果两个数字是相同的,我会根据名字对它们进行排序

为了实现它,我使用total_排序。但是,它不会返回预期的输出

import numpy as np
from functools import total_ordering


@total_ordering
class Person(object):
    def __init__(self, name, number):
        self.name=name
        self.number=number

    def __repr__(self):
        return "{}, {}".format(self.number, self.name)

    def __lt__(self, other):
        return self.number<other.number

    def __eq__(self, other):
         return (self.number==other.number and self.name==other.name) or self.number==other.number

    def __le__(self, other):
        return (self.number==other.number and self.name<other.name) or self.number<other.number

customList=[
    Person('object', 99),
    Person('michael', 1),
    Person('theodore', 21),
    Person('amazon', 21),
    Person('life', 42),
    Person('tree', 42)
]

a=sorted(customList)
print(a)
将numpy导入为np
从functools导入总排序
@总订单
类人(对象):
定义初始化(自我、姓名、编号):
self.name=name
self.number=number
定义报告(自我):
返回“{},{}”。格式(self.number,self.name)
定义(自身、其他):
返回self.number我会写

def __lt__(self, other):
    return (self.number, self.name) < (other.number, other.name)
def\uu lt\uuu(自身、其他):
返回(self.number,self.name)<(other.number,other.name)

类似地,对于
\uuuu eq\uuuu
\uuuu le\uuuu
而言,您的
\uu lt\uuuu
仅比较
数字
,并且在
排序
中仅使用
\uu lt\uuuu
。您应该使用
return(self.number,self.name)<(other.number,other.name)
我相信您的定义中有输入错误。它应该是(other.number,other.name),而不是(other.number,self.name)。这样它就能正常工作。def\uu lt\uu(self,other):返回(self.number,self.name)<(other.number,self.name)