Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将一位数和两位数混合排序_Python_Sorting_File Handling - Fatal编程技术网

Python 将一位数和两位数混合排序

Python 将一位数和两位数混合排序,python,sorting,file-handling,Python,Sorting,File Handling,我正在做一个游戏,你可以猜一首来自艺术家的歌和一些字母。我想创建一个高分列表,但是,我发现这很难,因为当我的分数为9&12时,python排序9比12高,因为1

我正在做一个游戏,你可以猜一首来自艺术家的歌和一些字母。我想创建一个高分列表,但是,我发现这很难,因为当我的分数为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()

只需在排序键lambda中转换为
int

scoresList.sort(key = lambda x: int(x.split('-')[0]))

只需在排序键lambda中转换为
int

scoresList.sort(key = lambda x: int(x.split('-')[0]))

如果我被允许对你的代码进行摇滚,我会按照以下思路做一些事情:

import operator

score = 10 # for instance

print(f'Score: {score}')
name = input('Enter your name: ')

scoresData = f'{score}-{name}'
print(scoresData)

with open('scores.txt', 'a') as database: # yea i know
    database.write(scoresData + '\n')

# ---
scoresList = {}
with open('scores.txt', 'r') as database:
    for row in database:
        score, player = row.split('-', 1)
        scoresList[player.strip('\n')] = int(score) # Remove \n from the player name and convert the score to a integer (so you can work on it as an actual number)

for row in sorted(scoresList.items(), key=operator.itemgetter(1)): # Sort by the value (item 1) of the dictionary
    print('Player: {} got a score of {}'.format(*row))
分拣是由
如果你想变得真正花哨,你可以:

import pickle
...
with open('scores.db', 'wb') as database:
    pickle.dump(scoreList, database)
或再次加载值:

with open('scores.db', 'rb') as database:
    scoreList = pickle.load(database)
这样就无需解析文本文件。您不必担心执行
player.strip('\n')
,因为没有任何换行符等需要处理。通过pickle进行内存转储的缺点是,我是一个“内存转储”,这意味着在适当的位置编辑值是不可能的


然而,另一个好的解决方案是使用它——如果您不习惯使用数据库,它会变得相当复杂,相当快。如果你愿意的话,那绝对是你最好的长期路线。

如果我被允许在你的代码上玩摇滚乐,我会采取以下行动:

import operator

score = 10 # for instance

print(f'Score: {score}')
name = input('Enter your name: ')

scoresData = f'{score}-{name}'
print(scoresData)

with open('scores.txt', 'a') as database: # yea i know
    database.write(scoresData + '\n')

# ---
scoresList = {}
with open('scores.txt', 'r') as database:
    for row in database:
        score, player = row.split('-', 1)
        scoresList[player.strip('\n')] = int(score) # Remove \n from the player name and convert the score to a integer (so you can work on it as an actual number)

for row in sorted(scoresList.items(), key=operator.itemgetter(1)): # Sort by the value (item 1) of the dictionary
    print('Player: {} got a score of {}'.format(*row))
分拣是由
如果你想变得真正花哨,你可以:

import pickle
...
with open('scores.db', 'wb') as database:
    pickle.dump(scoreList, database)
或再次加载值:

with open('scores.db', 'rb') as database:
    scoreList = pickle.load(database)
这样就无需解析文本文件。您不必担心执行
player.strip('\n')
,因为没有任何换行符等需要处理。通过pickle进行内存转储的缺点是,我是一个“内存转储”,这意味着在适当的位置编辑值是不可能的


然而,另一个好的解决方案是使用它——如果您不习惯使用数据库,它会变得相当复杂,相当快。如果你愿意,这绝对是你最好的长期路线。

这是因为你在比较字符串,在排序之前将它们转换为
int
,除非你对lambda感到满意,否则我不会使用它。您需要先将分数(字符串)转换为一个可行的整数。否则,你就注定要以这种方式比较字符串。一种方法是做
scorelist={};用于记录文件中的行;分数,名称=行分割('-',1);scorelist[name]=int(score)
并用于对词典结果进行排序。同时也使访问玩家名称变得更加容易。在大多数情况下,字典比列表快得多。也可以查看pickle。这是因为您正在比较字符串,所以在排序之前将它们转换为
int
,除非您对lambda感到满意,否则我不会使用它。您需要先将分数(字符串)转换为一个可行的整数。否则,你就注定要以这种方式比较字符串。一种方法是做
scorelist={};用于记录文件中的行;分数,名称=行分割('-',1);scorelist[name]=int(score)
并用于对词典结果进行排序。同时也使访问玩家名称变得更加容易。在大多数情况下,字典比列表快得多。也看看泡菜。