通过 class 方法按字母顺序排列列表

Alphabetizing a list through class methods

我一直在尝试使用 class 的 __str__ 方法打印列表,但在打印列表之前,必须通过 [=13] 之前的其他方法按字母顺序排列=] 方法在 class 中。为此提供的方法是:

def returnName(self):
    '''return the name of the player first last'''

    return(self.first + self.last)

def returnReverseName(self):
    '''return the name of the player as last, first'''
    reverseName = (str(self.last), str(self.first))
    return(reverseName)

def __eq__ (self, other):
    '''determine if this persons name is the same as the other personsname'''

    if self.returnReverseName == other.returnReverseName:
        return True
    else:
        return False


def __lt__(self,other):
    '''determine if this persons name is less than the other persons name alphabetically'''

    if str(self.returnReverseName) < str(other.returnReverseName):
        return True
    else:
        return False

def __gt__ (self, other):
    '''determine if this persons name is greater than the other persons name alphabetically'''

    if self.returnReverseName > other.returnReverseName:
        return True
    else:
        return False

def __str__(self):
    '''return a string of the persons name and their rating in a nice format'''

    Name1 = str(self.first) + ' ' + str(self.last)
    return ('%-20s %10.2f' % (Name1, self.rating))

其中姓名需要按姓氏、名字排序,但按名字姓氏顺序打印。方法 __lt____gt__ 是打印时应该排序的方法。到目前为止,我用来编译列表的代码是:

basicList.sort()
for line in playerDict:
    print(playerDict[line])

但这并没有利用列表,只是以无序的方式逐行打印字典。虽然它确实提到了方法,但它无法按字母顺序排列名称。我如何正确使用这些方法以正确的顺序打印字典?如果我需要使用播放器对象列表,我如何使用 __str__ 方法将其打印出来 post-alphabetization?

这段代码有很多错误和古怪之处,但它所做的第一件事实际上可能会导致错误的结果,那就是 __eq____gt__ 比较的是两个函数而不是两个名称。 __lt__,出于某种原因,比较了这两个函数的字符串表示。我不知道为什么它的行为与其他函数不同,但它仍然是错误的(只是方式不同)。

我对未使用排序列表并不感到惊讶 - 你永远不会使用它。

basicList.sort() # sort the list
for line in playerDict: # that's not basicList
    print(playerDict[line]) # still no basicList

您的 class 方法如果得到修复,可能如下所示:

def returnName(self):
    '''return the name of the player first last'''
    return(self.first + ' ' + self.last)

def returnReverseName(self):
    '''return the name of the player as last, first'''
    return self.last, self.first

def __eq__ (self, other):
    '''determine if this persons name is the same as the other personsname'''
    return self.returnReverseName() == other.returnReverseName()

def __lt__(self,other):
    '''determine if this persons name is less than the other persons name alphabetically'''
    return self.returnReverseName() < other.returnReverseName()

def __gt__ (self, other):
    '''determine if this persons name is greater than the other persons name alphabetically'''
    return self.returnReverseName() > other.returnReverseName()

def __str__(self):
    '''return a string of the persons name and their rating in a nice format'''
    return '%-20s %10.2f' % (self.returnName(), self.rating)

但您可能不需要这些。看起来 playerDict 是一个字典,它包含一些附加到 Player 对象值的键(我假设这是 class 名称)。只需迭代这些值并使用给定的排序键:

for entry in sorted(playerDict.values(), key=lambda x: x.last, x.first):
    print(entry)