我需要从学生测验文件中的分数中阅读,并在3次尝试中返回最高分数。我需要在Python中使用哈希/字典。这是我到目前为止所拥有的。
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}
如果您不需要为每次尝试存储分数,那么为什么不替换该值高于当前记录的尝试?
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)
给定代码的问题:
循环首先从文本标头开始,该文本标头未检查,然后再访问deqle for Student_id checks。打印在代码末尾的SCORE
值相同的值。平均无需脚本。
固定代码
这是示例代码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]), "nn")
样本输出:
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