我在做一个游戏,你猜一首歌的艺术家和一些信提供。我想创建一个高分列表,但是我发现这很困难,因为当我有诸如9&12这样的分数时,python对9的排序高于12,因为1<;9。如果可以的话,我需要一些帮助。在
print('Score: ' + str(score))
name = input('Enter your name: ')
scoreappender.append(str(score))
scoreAppender.append(name)
scoresData = '-'.join(scoreAppender)
print(scoresData)
scoresfile = open('scores.txt', 'a')
scoresfile.write(scoresData + '\n')
scoresfile.close()
scoresfile = open('scores.txt', 'r')
scoresList = []
for score in scoresfile:
scoresList.append(score)
scoresList.sort(key = lambda x: x.split('-')[0])
for score in scoresList:
print(score)
scoresfile.close()
如果允许我对你的代码进行摇滚,我会按照以下思路做些事情:
排序由[A]How do I sort a dictionary by value?
^{pr2}$如果你想玩得很花哨,你可以:
或再次加载值:
这样就不需要解析文本文件了。您不必担心执行
player.strip('\n')
,因为不会有任何新行等需要处理。通过pickle进行内存转储的缺点是,我是一个“内存转储”,这意味着在适当的地方编辑值是不可能的/直接的。在另一个好的解决方案是使用sqlite3,但是,如果您不习惯于使用数据库,它会变得相当复杂,相当快。如果你准备好了,那绝对是你长期以来最好的选择。在
只需转换为排序键lambda中的
int
:相关问题 更多 >
编程相关推荐