简体   繁体   English

如何仅显示文件中的前 5 个高分?

[英]How do I display just the top 5 highscores from my file?

This is the file with the scores:这是带有分数的文件:

Ishaan - 72
Jack - 84
Bob - 23
Louis - 77
Arnav - 56
Ben - 48
Ted - 39

So far I have sorted the file but I don't know how to display just the top 5 scores from the file.到目前为止,我已经对文件进行了排序,但我不知道如何只显示文件中的前 5 个分数。

ScoresFile2 = "/Users/KADAM BOYS/Desktop/Ishaan's Folder/Homework (Year 10)/Computing/Mock Project/Scores.txt"
ScoresWithNames = []
with open(ScoresFile2) as file2:
    for line in file2:
        ScoresWithNames.append(line.strip())
ScoresWithNames.sort(key=lambda x: int(x.split(" - ")[-1]))

This also has the same answer as your previous question, but modified:这也与您之前的问题具有相同的答案,但已修改:

l = ['Ishaan - 72', 'Jack - 84', 'Bob - 23', 'Louis - 77']
[' - '.join(k for k in j[::-1]) for j in sorted([i.split(' - ')[::-1] for i in l],reverse = True,key=lambda x: int(x[0]))][:5]

So if your sorted list is list1 :因此,如果您的排序列表是list1

top5 = list1[:5]

If you are using lambda:如果您使用的是 lambda:

ScoresWithNames.sort(key=lambda x: int(x.split(" - ")[-1]),reverse = True)
print(ScoresWithNames[:5])

Now if you want to print it with a new-line, you have two ways:现在如果你想用换行符打印它,你有两种方法:

for i in top5:
    print(i)

or:或者:

print('\n'.join(i for i in top5)) # or scoreswithnames 

An easy way to get the 5 highest scores is with most_common from collections.Counter :获得 5 个最高分的简单方法是使用来自collections.Countermost_common

from collections import Counter

with open("data.txt") as f:
    counts = Counter()
    for line in f:
        name, score = line.split(" - ")
        counts[name] += int(score)

    print(counts.most_common(5))

Output: Output:

[('Jack', 84), ('Louis', 77), ('Ishaan', 72), ('Arnav', 56), ('Ben', 48)]

If you want your scores formatted back into "name - score" format:如果您希望您的分数重新格式化为"name - score"格式:

print([f"{name} - {score}" for name, score in counts.most_common(5)])
# ['Jack - 84', 'Louis - 77', 'Ishaan - 72', 'Arnav - 56', 'Ben - 48']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python 对我的文件(高分)进行排序? - How do I sort my File (highscores) with python? 如何在Python(Pygame)中显示逗号分隔值(.txt)文件中的前10个高分 - How to display top 10 highscores from a comma separated value (.txt) file in Python (Pygame) 如何在正确的用户名旁边打印我的高分(从文本文件)? - How can I print my highscores (from a text file) along side the correct username? 如何在 pygame 的屏幕顶部显示分数? - How do I display a score at the top of my screen in pygame? 我正在尝试将前三名高分保存到单独行的txt文件中 - I'm trying to save the top three highscores to a txt file on seperate lines 这是什么错误,因此我如何显示 .csv 文件中的前 5 个分数? - What is this error and how do I therefore display top 5 scores from a .csv file? 如何开始从python的顶部读取文件? - How do I start reading a file from the top in python? 如何使文件属性显示在属性窗口中? - How do I get my file properties to display in the properties window? 如何将html文件加载/显示到QTextBrowser小部件中? - How do I load/display an html file into my QTextBrowser widget? 如何在Python上仅显示列表中没有逗号和括号的数字? - How do I just display the numbers from the list without the comma and brackets on Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM