使用python计算测验数据的平均值(疑难解答)

2024-03-28 22:42:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要读入学生测验文件中的分数,并返回3次测验中的最高分数。我需要在python中使用hash/dictionary。这是我到目前为止的情况。你知道吗

STD_ID  = {}
ATTEMPTS = {}
SCORE= {}

f= open("std_attempt_score.txt", "r")
line = f.readline()
for line in f:
    line = line.rstrip()
    ents = line.split("\t")
    did = ents[1]
    if did in STD_ID:
            ATTEMPTS[did] += 3
            SCORE[did] += int(ents[2])
    else:
            STD_ID[did]  = ents[2]
            ATTEMPTS[did] = 3
            SCORE[did] = int(ents[2])


for  key in STD_ID:
    print("Avg score for Student", key, "=",SCORE)

文件中的文本数据。你知道吗

FILE
STD_ID  ATT_NUM SCORE
S23Y    1   85
S03X    1   80
S34Z    1   19
S54M    1   23
S34Z    2   25
S01X    1   79
S03X    2   10
S23Y    2   09
S34Z    3   92
S54M    2   96
S23Y    3   74
S54M    3   65
S03X    3   54

我的结果如下:

Avg score for Student 1 = {'1': 286, '2': 140, '3': 285}
Avg score for Student 2 = {'1': 286, '2': 140, '3': 285}
Avg score for Student 3 = {'1': 286, '2': 140, '3': 285}

Tags: inidforlinestudentavgscorestd
2条回答

如果您不需要存储每次尝试的分数,那么如果该值高于当前记录的尝试,为什么不直接替换该值呢?你知道吗

students = {}

f = open("std_attempt_score.txt", "r")
line = f.readline()
for line in f:
    line = line.rstrip()
    ents = line.split(",")
    student_id = ents[0]
    attempt_score = int(ents[2])

    score = students.setdefault(student_id, attempt_score)

    if attempt_score > score:
        students[student_id] = attempt_score


for student_id, score in students.items():
    print("Highest score for Student", student_id, "=", score)

给定代码的问题:

循环以一个文本头开始,在转到if else语句进行学生id检查之前不检查该文本头代码编号在脚本中取平均值。

固定代码

下面是一个示例代码
STD_ID  = {}
ATTEMPTS = {}
SCORE= {}

f= open("std_attempt_score.txt", "r")
line = f.readline()
for line in f:
    line = line.rstrip()
    entss = " ".join(line.split())
    ents = entss.split(" ")
    did = ents[0]
    if not line.startswith("STD_ID"): # check for the header you definitely want to skip this line
            if did in STD_ID :
                    ATTEMPTS[did] += 1
                    SCORE[did].append(int(ents[2])) #polulate student_id with marks
            else:
                    STD_ID[did] = [ents[0]] #Start Dictionary with student ID and marks
                    ATTEMPTS[did] = 1
                    SCORE[did] = [int(ents[2])]

for key in sorted(STD_ID):
    if len(SCORE[key]) < 3:
        dumValues = [0] * (3 - len(SCORE[key]))
        SCORE[key] = SCORE[key] + dumValues  # add 0's in un-attempted quizzes.
    print("Student ID {0} Score Summary : \n".format(key))
    print("Top 3 Quiz : ", sorted(SCORE[key], reverse=True)[:3])
    print("Avg score of top 3 quiz : " , sum(sorted(SCORE[key], reverse=True)[:3]) / 3)
    print("Quiz With Highest Marks out of 3 Top Quizzes : ", sorted(SCORE[key], reverse=True)[0])
    print("Total Marks in 3 Attempts : ", sum(sorted(SCORE[key], reverse=True)[:3]), "\n\n")

样本输出:

Student ID S01X Score Summary : 

Top 3 Quiz :  [79, 0, 0]
Avg score of top 3 quiz :  26.333333333333332
Quiz With Highest Marks out of 3 Top Quizzes :  79
Total Marks in 3 Attempts :  79 


Student ID S03X Score Summary : 

Top 3 Quiz :  [80, 54, 10]
Avg score of top 3 quiz :  48.0
Quiz With Highest Marks out of 3 Top Quizzes :  80
Total Marks in 3 Attempts :  144 


Student ID S23Y Score Summary : 

Top 3 Quiz :  [85, 74, 9]
Avg score of top 3 quiz :  56.0
Quiz With Highest Marks out of 3 Top Quizzes :  85
Total Marks in 3 Attempts :  168 


Student ID S34Z Score Summary : 

Top 3 Quiz :  [92, 25, 19]
Avg score of top 3 quiz :  45.333333333333336
Quiz With Highest Marks out of 3 Top Quizzes :  92
Total Marks in 3 Attempts :  136 


Student ID S54M Score Summary : 

Top 3 Quiz :  [96, 65, 23]
Avg score of top 3 quiz :  61.333333333333336
Quiz With Highest Marks out of 3 Top Quizzes :  96
Total Marks in 3 Attempts :  184 

相关问题 更多 >