所以我需要把高分排序,下面是我已经掌握的代码:
def sortscores():
namelist = []
scorelist = []
hs = open("hst.txt", "r")
hscounter = 0
for line in hs:
if counter%2 !=0:
name = line
othername = name[0:len(name)-1]
namelist.append(othername)
else:
scorelist.append(int(line))
这会将名字和分数放入列表中,所以现在我需要对它们进行排序,但是我不能使用.sort()函数,因为我必须自己编写排序,所以有人能告诉我如何进行排序吗?(将分数按降序排序,同时保持名字的正确分数)
您可以复制dict,找到最大值,将键保存到列表中,从dict中删除键,然后再这样做,直到复制的dict为空。在
如果您将高分存储在
(name, score)
元组中,则可以轻松地将它们放在一起。因为您需要自己编写sort函数,所以看看在另一个问题中使用元组的示例可能会有所帮助。下面是一个简单地查找最大分数,同时保持名称和分数在一起的示例。在首先,设置数据。您可以使用
zip
进行此操作输出:
^{pr2}$现在找到最大条目:
输出:
相关问题 更多 >
编程相关推荐