我如何更改 __lt__ 魔法方法才能使该算法起作用? - Python

How do I change the __lt__ magical method so this algorithm can work ? - Python

Python排序问题

我有一个排序算法问题,它包括对 sublclass Priority Person 的对象进行排序(这个对象将被放置在一个列表中,目标是改变 sub[=29 的 lt 方法=] 优先人对列表上的 .sort() 方法有影响),如果优先人的对象有一个由参数 True 描述的缺陷,它应该被认为比获得参数 False 的优先人对象更小(这意味着此人没有任何缺陷),如果两者都有缺陷或他们都没有缺陷,则应考虑所涉及的优先人员的名字的字典顺序。 Priority Person 的 sublclass 的主要 class 是我上面显示的 class Person :

class Person(object):

    def __init__(self, name):
        """Create a person"""
        self.name = name
        try:
            lastBlank = name.rindex(' ')
            self.lastName = name[lastBlank + 1:]
        except:
            self.lastName = name
        self.birthday = None

    def __lt__(self, other):
        """Returns True if self's name is lexicographically
           less than other's name, and False otherwise"""
        return self.name < other.name

    def __str__(self):
        """Returns self's name"""
        return self.name

class PriorityPerson(Person): # the __lt__ part of this class does not work and I don't know why?

   def __init__(self,name,deficiencia):
        super().__init__(name)
        self.deficiente = deficiencia
    
    
    def __lt__(self, other):
        if self.deficiente and other.deficiente and self.name< other.name:
            return self.name < other.name
        elif self.deficiente and other.deficiente and other.name < self.name:
            return other.name< self.name
        elif self.deficiente and not other.deficiente and self.name < other.name:
            return self.name < other.name
        elif self.deficiente and not other.deficiente and other.name < self.name:
            return self.name< other.name
        elif not self.deficiente and other.deficiente and self.name < other.name:
            return other.name < self.name
        elif not self.deficiente and other.deficiente and other.name < self.name:
            return other.name < self.name
        elif not self.deficiente and not other.deficiente and self.name < other.name:
            return  self.name < other.name
        elif not self.deficiente and not other.deficiente and other.name < self.name:
            return other.name < self.name

实现示例:

p1 = PriorityPerson("John Smith", False)
p2 = PriorityPerson("Sam Mendes", False)
p3 = PriorityPerson("Grandmother Anne", True)
p4 = PriorityPerson("Stephen Hawking", True)
p5 = PriorityPerson("Betty Large", True)

listaPessoas = [p1,p2,p3,p4, p5]
listaPessoas.sort()
for p in listaPessoas:
   print(p)

正确输出:

Betty Large
Grandmother Anne
Stephen Hawking
John Smith
Sam Mendes

我的错误输出:

Betty Large
Stephen Hawking
Grandmother Anne
Sam Mendes
John Smith

如有任何帮助,我们将不胜感激。 谢谢。

您基本上想按优先级排序,然后按名称排序。所以,你必须首先比较不同的优先级,并且只有相同的优先级,对于 name:

def __lt__(self, other):
    if self.deficiente != other.deficiente: # yes, you can compare bools!
        return self.deficiente
    else:
        return self.name < other.name

我希望这有效

class Person(object):
    def __init__(self, name):
        """Create a person"""
        self.name = name
        try:
            lastBlank = name.rindex(' ')
            self.lastName = name[lastBlank + 1:]
        except:
            self.lastName = name
        self.birthday = None

    def __lt__(self, other):
        """Returns True if self's name is lexicographically
        less than other's name, and False otherwise"""
        return self.name < other.name

    def __str__(self):
        """Returns self's name"""
        return self.name


class PriorityPerson(Person):
    def __init__(self, name, deficiencia):
        super().__init__(name)
        self.deficiente = deficiencia

    def __lt__(self, other):
        if self.deficiente and not other.deficiente:
            # If self is VIP and other is not, just return True
            return True
        elif not self.deficiente and other.deficiente:
            # If other is VIP and self is not, just return False
            return False

        # On equal priority, normal compare
        return super().__lt__(other)

p1 = PriorityPerson("John Smith", False)
p2 = PriorityPerson("Sam Mendes", False)
p3 = PriorityPerson("Grandmother Anne", True)
p4 = PriorityPerson("Stephen Hawking", True)
p5 = PriorityPerson("Betty Large", True)

listaPessoas = [p1,p2,p3,p4, p5]
listaPessoas.sort()
for p in listaPessoas:
   print(p)

输出:

Betty Large
Grandmother Anne
Stephen Hawking
John Smith
Sam Mendes