简体   繁体   English

平均数从最高到最低

[英]Highest to Lowest from an average

I currently have a piece of code which finds an average from a file, so far I can find the average but cant sort this afterwards, below is my code which finds the average:我目前有一段代码可以从文件中找到平均值,到目前为止我可以找到平均值,但之后无法对其进行排序,下面是我的代码,它可以找到平均值:

path = 'team1scores.csv'
with open(path) as f:
    entries = collections.Counter()
    total_scores = collections.Counter()
    for name,score in csv.reader(f):
        score = int(score)
        total_scores[name] += score
        entries[name] += 1

    for name in sorted(entries):
        ave_score = total_scores[name] / entries[name]
        print(name,ave_score)

In the same program I have used a highest to lowest sort at another point so would I somehow be able to add this...在同一个程序中,我在另一个点使用了从最高到最低的排序,所以我能以某种方式添加这个......

path = 'team1cores.csv'
    with open(path) as f:
        entries = sorted(csv.reader(f), key=itemgetter(1), reverse=True)
    for name,score in entries:
        print(name,score)

...To the end of the average snippet. ...到平均片段的末尾。 I have tried several different ways without any succession.我尝试了几种不同的方法,没有任何连续性。 Once the average part of the program is finished it returns these, this is what I need to sort.程序的平均部分完成后,它会返回这些,这就是我需要排序的内容。

Derek 4.0
Fred 9.0
George 7.0
Jake 3.6666666666666665

Hopefully it is a simple problem to fix.希望这是一个简单的问题来解决。

You should think better the structure of your code: to sort the file reading process makes no sense... if you want to sort the average scores, you must register this information in a specific variable.您应该更好地考虑代码的结构:对文件读取过程进行排序是没有意义的……如果您想对平均分数进行排序,则必须将此信息注册到特定变量中。

I believe you want to do something like this:我相信你想做这样的事情:

path = 'team1scores.csv'

entries = collections.Counter()
total_scores = collections.Counter()

with open(path) as f:
    for name,score in csv.reader(f):
        score = int(score)
        total_scores[name] += score
        entries[name] += 1

averages = collections.Counter()
for name in entries.keys():
    averages[name] = total_scores[name]/entries[name]

# `most_common(n)` will display the sorted `n` highest values
for name, score in averages.most_common():
    print(name, score)

# to sort the `n` from lowest, used most_common()[:`-n-1`:-1]
n = 2
for name, score in averages.most_common()[:-n-1:-1]:
    print(name, score)

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

相关问题 从最高到最低的平均值 - Average from Highest to lowest 从文本文件按平均,最高和最低排序数据 - Sorting data, by average, highest and lowest, from a text file 按字母顺序从 csv 中排序数据,从最高到最低和平均 - Sorting data from a csv alphabetically, highest to lowest and average 从字典中从最高到最低排序的每个学生(关键)从3(值)列表中输出最高/平均分数? - Outputting Highest/Average Score From List Of 3 (Value) For Each Student (Key) In A Dictionary Sorted From Highest To Lowest? 从文本文件的最高到最低? - Highest to Lowest from a textfile? 按字母,平均,从高到低的顺序排列分数和名称 - Sorting scores and names alphabetical, average, highest to lowest 打印文件的最高、平均、最低分 - Print Highest, Average, Lowest score of a file 按平均值从高到低对字典排序 - Sort a dictionary by average values highest to lowest 如何使用字典对文本文件中的结果按平均,从最高到最低的字母顺序进行排序? - How can I use a dictionary to sort results from a text file alphabetically, by average and by highest to lowest? 如何将用户输入的姓名和年龄列表排序为具有相应姓名的平均年龄、最高年龄和最低年龄? - How to sort a list of names and ages as input from the user into average age, highest, and lowest age with corresponding name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM