python - 按字母顺序从最高到最低和平均值对 csv 中的数据进行排序

标签 python sorting csv average

这是我的 currently unresolved question 的下一步我试图对 3 个不同团队的得分进行排序。我对 python 的了解非常有限,因为我是编程新手,所以解决当前项目的问题相当困难。

首先,我需要示例数据(如下所示),这些数据分为两个单元格,并根据名称按字母顺序排序,我将在 3 个不同的文件中为 3 个不同的团队提供此数据。我还尝试根据分数从最高到最低进行排序,到目前为止这对我来说非常困难。

Jake,5
Jake,3
Jake,7
Jeff,6
Jeff,4
Fred,5

我尝试做的第三种也是最后一种排序方法是按平均值。为此,我尝试这样做,如果用户输入该名称 2 或 3 次(因为程序将存储每个用户的最后 3 个分数,这是一个 currently unresolved 问题),那么它将添加他们的分数,然后除以那里有多少个。不幸的是,这对我来说非常困难,我很难获得任何输出,尽管我有一个想法,这会将他们的平均分数打印到单独的文件中,然后重新读取分数。

到目前为止我的当前布局如下所示:

admin_data = []
team_choice = input("Choose a team to sort")
if team_choice == 'Team 1':
    path = 'team1scores.csv'

elif team_choice == 'Team 2':
    path = 'team2scores.csv'

elif team_choice == 'Team 3':
    path = 'team3scores.csv'

else:
    print("--Error Defining File Path--")

print("As an admin you have access to sorting the data")
print("1 - Alpahbetical")
print("2 - Highest to Lowest")
print("3 - Average Score")

admin_int = int(input("Choose either 1, 2 or 3?"))

if sort_int == 1 and team_choice == 'Team 1':
    do things

elif sort_int == 2 and team_choice == 'Team 1':
    do things

elif sort_int == 3 and team_choice == 'Team 1':
    do things

程序的这一部分将用于每个文件,但没有运气为我需要的每种不同的排序方式生成任何解决方案。如果 first part 的答案,我也将不胜感激我的项目的也得到了解答。

编辑 (16:43): 我已经成功完成了程序的最高到最低部分,但正在打印:

[['Fred', '9'], ['George', '7'], ['Jake', '5'], ['Jake', '4'], ['Derek', '4'], ['Jake', '2']]

因此,如果这是我读取数据的格式,那么如果它们位于这样的数组中,我将如何读取文件中的重复名称并添加分数?

最佳答案

第一步是将问题分解为小步骤:

  1. How to open and handle the file (使用该部分底部的 with 语句)
  2. How to traverse a csv file
  3. How to sort the entries
  4. How to sort by the second value of each row
  5. How to print each element of a list on a separate line
  6. How to count total scores

扩展最后一个,您可以将分数以及每个名称的条目数相加,如下所示:

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

然后您可以使用total_scores[name]/条目[name]计算每个人的平均分数

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

其他两个操作非常简单,只需执行上面列出的几个步骤即可。

import csv
import collections
from operator import itemgetter

...

if sort_int == 1:
    with open(path) as f:
        reader = csv.reader(f)
        for name, score in sorted(reader):
            print(name,score)

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

elif sort_int == 3:
    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)

如果您想将最高到最低的平均分应用于平均分,那么您需要引用所有平均值,例如dict:

ave_scores = {}
for name in sorted(entries):
    ave_score = total_scores[name] / entries[name]
    ave_scores[name] = ave_score

for name,ave_score in sorted(ave_scores.items(), key = itemgetter(1), reversed=True):
    print(name,ave_score)

关于python - 按字母顺序从最高到最低和平均值对 csv 中的数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36967883/

相关文章:

python - numpy.polyfit 给出空残差数组

c# - 如何在排序数组中定位一个点?

python - 如何在Python中安装guess_language包

r - 如何在 R 中按一个变量对数据帧进行排序,同时对其他变量进行分组

c++ - std::sort with personal 类返回段错误

python - 如何使用 Python Pandas 处理包含数百万条记录的 DataFrame?

python - Pandas 误解 CSV 文件中的日期列

PHP 将文件读入数组

python - 简单的django网站搜索

python - 通过检测 NaN 出现的位置,对其他列进行数学运算,将 NaN 填充到列中