我已经创建了从文本文件中提取3个分数的代码,并对这些名字进行排序 按字母顺序排列,但我很难知道如何按平均值将得分最高的排序为得分最低的排序
#Opens up the text file "class1.txt"
class1 = open('class1.txt', "r")
#Reads the lines in class as' results file
classread = class1.readlines()
#Alerts the user that the names in class1.txt will be sorted alphabetically
print("Names sorted alphabetically")
print()
print("Name Last 3 scores")
print()
#Sorts the list alphabetically
for word in sorted(classread):
word = word.rstrip()
print(word)
class1.close()
函数
sorted
可以接收一个名为key
的关键字参数,它是一个为列表中的每个元素返回一个可比较参数的函数如果有两个列表,其中一个是名称,另一个是平均值,则可以使用:
如果您有
name: average
形式的词典,您可以使用:相关问题 更多 >
编程相关推荐