在具有类的列表上使用python Sorted函数

use*_*861 1 python sorting list

我目前正在尝试使用内置函数Sorted来对具有名为scores的类属性的列表进行排序.

这里排序的函数只与列表一起工作,它按升序正确地对分数进行排序.

Scores = [1,5,19,0,900,81,9000]


print(sorted(Scores))
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试将此函数应用于具有类属性的列表时,将返回错误:AttributeError:'list'对象没有属性'Score'

这是我的代码:

print(sorted(RecentScores.Score))
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感激.

下面是我的类初始化和列表RecentScores

class TRecentScore():
  def __init__(self):
    self.Name = ''
    self.Score = 0

RecentScores = [1,5,0]
Run Code Online (Sandbox Code Playgroud)

Hen*_*son 5

你没有在你的班级中使用你正在做的事情只是声明一个包含一些数字的列表,然后你试图调用该Score列表对象上的属性.

class TRecentScore():
    def __init__(self, name, scores):
        self.name = name
        self.scores = scores

recent_scores = TRecentScore('Sorted Scores', [1,5,0])
print(sorted(recent_scores.scores))
Run Code Online (Sandbox Code Playgroud)