我有三个功能,一个用于计算实验平均值,一个用于加权分数,一个用于程序平均值。这些函数计算用户选择的特定学生的分数。 (例如,仅为Ablao而非其他学生计算实验室平均值)
Ablao 3 74 96 72 88 71 80 83 77 90 88 95 71 76 94 80 74 98 77
Anderson 3 76 92 98 95 92 76 93 97 85 76 85 93 82 88 75 84 92 77
Aspinwall 1 86 74 78 97 86 94 73 95 74 91 75 94 83 99 83 78 88 96
Bacon 4 72 95 81 80 89 88 100 87 87 81 79 77 75 83 87 96 72 95
该文件中有60名学生的信息。名字后面的第一个数字代表学生的成绩。 1 =新生2 =大二等。
我需要创建一个新功能,它将使用我现有的功能来计算所有学生的平均值,以及每个年级的学生的平均值,然后平均这些平均值,找到最大值和最小值。
到目前为止我为此功能编写的代码不起作用,我不知道如何开始解决问题。
with open(FileName, 'r') as f:
line = f.readlines()
student_status=(float(line.split()[1]))
def make_lists_of_status():
if (student_status==1):
Freshman.append(student_scores)
elif (student_status==2):
Sophomores.append(student_scores)
elif (student_status==3):
Juniors.append(student_scores)
elif (student_status==4):
Seniors.append(student_scores)
预期输出应该像
This option is for viewing statistics sorted by the year of student.
Please select one of the following options:
a for ALL YEARS
b for FRESHMAN
c for SOPHMORES
d for JUNIORS
e for SENIORS
Enter your choice here: e
For Seniors:
High/Low/Mean of all Weighted Scores: 89.53999999999999 / 81.60000000000001 / 86.15736842105264
High/Low/Mean of all Lab Averages: 89.6 / 80.2 / 86.21052631578948
High/Low/Mean of all Program Averages: 94.33333333333333 / 77.0 / 86.78947368421052
答案 0 :(得分:0)
这可以用字典来完成。
students = {}
with open("inCSV.txt", "r") as f:
for line in f.readlines():
data = line.rstrip().split(" ")
student_name = data[0]
student_grade = data[1]
student_data = [int(x) for x in data[2:]]
student_avg = sum(student_data[2:])/float(len(student_data[2:]))
# Set the students name to a tuple of student's grade and average
students[student_name] = (student_grade, student_avg)
# Sort the students by their averages. (Descending order)
# Add reverse=True to have it in ascending order
print sorted(students.keys(), key=lambda x: students[x][1])
修改强>
它仍然可以以类似的方式完成。
students = {}
def weighted_scores(x):
return x
with open("inCSV.txt", "r") as f:
for line in f.readlines():
data = line.rstrip().split(" ")
student_name = data[0]
student_grade = data[1]
student_data = [int(x) for x in data[2:]]
student_program_avg = sum(student_data[2:])/float(len(student_data[2:]))
# Edit this part to your code
student_weighted_scores = weighted_scores(data[2:])
student_lab_averages = lab_average(data[2:])
students[student_name] = (student_grade,
student_weighted_scores,
student_lab_averages,
student_program_avg)
print students
def get_level(x):
ret_dict = {}
for k, v in students:
if v[0] == x:
ret_dict[k] = v
def choose(x):
people = get_level(x)
overall = []
for s in range(1, 4):
# Get grades of either weighted scores, lab averages or just average.
# Sort grades in descending order
grades = sorted(people.keys(), key=lambda x: people[x][s])
highest = grades[0]
lowest = grades[-1]
mean = sum(grades[2:])/float(len(grades[2:])
overall.append((highest, lowest, mean))
# overall[0] => weighted scores
# overall[1] => lab averages
# overall[2] => program average
# overall[x][0] => highest of x
# overall[x][1] => lowest of x
# overall[x][2] => mean of x
print "For %i level:" % (x)
print "High/Low/Mean of Weighted Scores: %i / %i / %i" % (overall[0][0], overall[0][1], overall[0][2])
print "High/Low/Mean of Lab Averages: %i / %i / %i" % (overall[1][0], overall[1][1], overall[1][2])
print "High/Low/Mean of Weighted Scores: %i / %i / %i" % (overall[2][0], overall[2][1], overall[2][2])
# Let's say you picked seniors:
choose(4)
答案 1 :(得分:0)
我认为这可以满足您的需求:
scores_student = {}
scores_grade = [[],[],[],[]]
scores_all = []
with open("students.txt", "r") as f:
for line in f:
# Parse the line
toks = line.split()
name = toks[0]
grade = int(toks[1])
scores = [float(tok) for tok in toks[2:]]
# Save scores both per student, in that student's grade, and total
scores_student[(name,grade)] = scores
scores_grade[grade-1] += scores
scores_all += scores
# Print individual student info
print "Students"
for name,grade in sorted(scores_student):
score = scores_student[(name,grade)]
print "%-10s %d mean %4.1f max %3d min %3d" % (name, grade, sum(score)/len(score), max(score), min(score))
print
print "Summary"
for grade, score in enumerate(scores_grade):
if len(score) > 0:
print "grade %d mean %4.1f max %3d min %3d" % (grade+1, sum(score)/len(score), max(score), min(score))
print "all mean %4.1f max %3d min %3d" % (sum(scores_all)/len(scores_all), max(scores_all), min(scores_all))