【问题标题】:How to calculate percentage and average of test scores in a 2D list without using libraries like pandas or numpy如何在不使用 pandas 或 numpy 等库的情况下计算二维列表中测试分数的百分比和平均值
【发布时间】:2022-11-21 06:07:52
【问题描述】:

我有一个测试成绩的 csv 数据。当前程序能够将此数据读入二维列表,测试不合格。后来我创建了一个函数来删除行外的测试,因此只能显示学生的分数。我现在正在努力编写一个可以打印分数的函数,以便每个学生的百分比出现在单独的输出行中。

到目前为止我的代码

def getData():

   with open("testscores.csv","r") as file:
    lineArray = file.read().splitlines()
    matrix = []
    for line in lineArray:
      matrix.append(line.split(","))

    return matrix

def fullScores(matrix):
  matrix.pop(0)

  return matrix

def printscores(matrix):

  for counter in matrix:
    for values in counter:
      print(values, end= " ")
    print()
  

matrix = getData()
matrix = fullScores(matrix)
print()
printscores(matrix)

输出

Bob 10 9 7 8 10 9 9 9 10 8 8 10 9 9 
Sue 8 8 8 9 4 8 9 7 8 3 10 10 7 9
Jan 6 6 0 5 7 9 4 7 8 5 7 1 5 9
Sam 8 8 8 7 7 7 9 9 9 9 8 9 10 8
Tom 9 9 9 9 9 9 9 9 9 10 9 9 9 9

预期产出

Bob 100% 90% 70% 80% 100% 90% 90% 90% 100% 80% 80% 100% 90% 90% Average = 89%
Sue 80% 80% 80% 90% 40% 80% 90% 70% 80% 30% 100% 100% 70% 90% Average = 77%
...

数据

Testoutof,10,11,12,11,10,11,9,10,10,11,10,12,10,9
Bob,10,9,7,8,10,9,9,9,10,8,8,10,9,9
Sue,8,8,8,9,4,8,9,7,8,3,10,10,7,9
Jan,6,6,0,5,7,9,4,7,8,5,7,1,5,9
Sam,8,8,8,7,7,7,9,9,9,9,8,9,10,8
Tom,9,9,9,9,9,9,9,9,9,10,9,9,9,9

【问题讨论】:

  • 为什么只丢弃第一行?这就是告诉您应该用来计算每列百分比的数字。您似乎没有尝试计算百分比和平均值。提问前请自己尝试一下。如果您已经尝试过,您可能有比“我该怎么做?”更具体的问题,所以请问这个问题。
  • 正确的。这只是为了避免向用户显示错误,但没关系,我们可以保留它。任何提示或建议如何计算百分比和平均值?谢谢。

标签: python csv


【解决方案1】:

当然,有很多方法可以做到这一点,但这里有一个可能的解决方案。我只使用列表和元组。使用字典,您将有一种更优雅的方式来访问数据。

students = []
reference_scores = []

def get_data():
    with open("./data/testscores.csv", "r") as file:
        lineArray = file.read().splitlines()        

        for line in lineArray:
            raw_data = line.split(",")
            name = raw_data[0]
            scores = []
            percentages = []
            average_percentage = 0

            if name != 'Testoutof':
                scores = raw_data[1:]

                for index, score in enumerate(scores):
                    percentage = get_percentage(int(score), int(reference_scores[index]))
                    average_percentage += percentage
                    percentages.append(percentage)
                average_percentage //= len(scores)
                students.append( (name, percentages, average_percentage) )
            else:
                reference_scores = raw_data[1:]
    return students

def get_percentage(score, reference):
    return (score * 100) // reference

def print_scores(data):
    for obj in data:
        score_string = ''
        for score in obj[1]:
            score_string += f'{score}% '    
        print(f'{obj[0]} {score_string} Average = {obj[2]}')

students = get_data()
print_scores(students)

# Output
Bob 100% 81% 58% 72% 100% 81% 100% 90% 100% 72% 80% 83% 90% 100%  Average = 86
Sue 80% 72% 66% 81% 40% 72% 100% 70% 80% 27% 100% 83% 70% 100%  Average = 74
Jan 60% 54% 0% 45% 70% 81% 44% 70% 80% 45% 70% 8% 50% 100%  Average = 55    
Sam 80% 72% 66% 63% 70% 63% 100% 90% 90% 81% 80% 75% 100% 88%  Average = 79 
Tom 90% 81% 75% 81% 90% 81% 100% 90% 90% 90% 90% 75% 90% 100%  Average = 87

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 2022-01-12
    • 2020-10-10
    • 2021-03-03
    • 2018-08-20
    • 1970-01-01
    相关资源
    最近更新 更多