我如何改变 __lt__ magical 方法,使这个算法能够工作?- 语法

问题描述 投票:0回答:1

Python排序问题

我有一个排序算法的问题,包括对子类Priority Person的对象进行排序(这些对象将被放置在一个列表中,目标是改变子类priority person的lt方法,以影响列表中的.sort()方法)。 sort()方法的影响),如果一个Priority Person的对象有一个参数True描述的缺陷,它应该被认为比一个得到参数False的优先级人对象的缺陷少(这意味着这个人没有任何缺陷),如果两者都有缺陷或两者都没有缺陷,应该考虑所涉及的优先级人的名字的词法顺序。优先人的子类的主类就是我上面展示的类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

任何帮助都将是非常感激的.谢谢你.

python class sorting object subclass
1个回答
1
投票

我希望这个问题能解决

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

0
投票

基本上是按优先级排序,其次是按名字排序。所以,你必须先比较不同的优先级,然后才比较相同优先级的名字。

def __lt__(self, other):
    if self.deficiente != other.deficiente: # yes, you can compare bools!
        return self.deficiente
    else:
        return self.name < other.name
© www.soinside.com 2019 - 2024. All rights reserved.