【问题标题】:Sorting data from a csv alphabetically, highest to lowest and average按字母顺序对 csv 中的数据进行排序,从最高到最低和平均
【发布时间】:2016-08-26 08:36:51
【问题描述】:

这是我的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']]

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

【问题讨论】:

  • 从高到低排序可以通过this answer 解决,并将reverse=True 应用于sorted,使其从高到低。当用户选择Average Score时,假设会发生什么,它会计算平均值还是排序?
  • 它应该读取文件,如果一个名称不止一次,它将把它们加在一起并除以它们的数量。之后我的想法是将新结果写入另一个文件,然后从最高到最低进行排序。这有意义吗?

标签: python sorting csv average


【解决方案1】:

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

  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] / entries[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)

【讨论】:

  • 前两个工作但平均部分存在问题: Traceback(最近一次调用最后一次):文件“C:/Users/Jake/Downloads/Task 1 (1).py”,第 182 行,在 中为名称,在 csv.reader(f) 中得分:TypeError: argument 1 must be an iterator
  • 如何将最高到最低添加到平均部分的末尾?
  • 我添加到答案@JakeKalcher。
猜你喜欢
  • 2015-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-26
  • 2011-01-15
  • 2011-12-08
  • 1970-01-01
  • 2023-03-11
相关资源
最近更新 更多