在Python中循环使用字典值

时间:2013-11-15 19:21:15

标签: python function loops dictionary

我有一个脚本,可以计算课程中所有学生的所有测验,作业和考试成绩的平均值。然后,我希望它比较每个学生在每个项目上的表现与使用词典的项目的平均值相比较。我怎么能这样做?

from __future__ import division

def main():
    quiz_averages = quiz_average(remove_zeros([57,0,36,29,38,31,33,0,42,0,52,28,0,50,26,97,80,63,8,36,33,39,12,36,12,59,75,61,57,39,18,9,19,35,75,5,18,0,56,0,24,0,9],
                                              [83,0,64,45,66,82,52,0,43,0,53,49,43,82,44,74,74,58,52,52,60,18,24,34,19,42,72,65,79,99,75,24,35,70,74,43,55,0,82,0,34,0,22],
                                              [69,0,38,16,16,16,55,0,28,0,44.5,31,0,33,52,75,46,33,36,0,49,34.5,35,79.5,13,27,31,52,40.5,64.5,15,0,31,47,26,0,36,0,68,0,64.5,0,20],
                                              [86,0,95,74,90,53,32,0,79,0,38,63,0,42,61,0,0,70,62,78,0,60,47,89,75,62,84,62,71,80,73,0,31,25,74,0,77,0,90,0,78,0,25]))
    student_quiz_scores = {'John':[61,65,52,62],'Kate':[87,56,73,24],'Mike':[97,54,84,42],'Danielle':[39,100,35,75],'Roger':[78,65,37,42]}
    print_quiz_averages(student_quiz_scores,quiz_averages)

    homework_averages = homework_average(remove_zeros([74,70,62,98,70,83,24,70,87,14,67,80,93,72,65,90,23,43,95,53,66,98,100,95,82,88,91,90,55,64,71,86,66,89,38,88],
                                                      [80,71,58,68,80,71,3,47,60,65,72,87,36,67,57,61,32,29,37,46,73,72,99,67,77,93,93,40,68,84,53,74,84,52],
                                                      [44,25,62,85,77,80,45,80,70,25,80,73,53,56,82,70,67,36,46,82,85,40,85,42,64,84,80,58,63,73,84,89,87,55,82],
                                                      [93,70.5,59,94,97,98.5,70,94,70.5,86,97.5,100,87,77.5,71,79,86,93.5,67,50,94,94,73.5,100,82,81,99.5,77.5,91.5,75.5,94,94,45,91.5],
                                                      [93.5,65,71,96,96,95,78,96,96,76,94,91.5,48,74,96,75,95,49,89,94,95,98,96,62,61,94,68,91,96,80,61,93],
                                                      [79,78,74,97.5,88,99.5,77,96,94,87.5,94,96.5,55,94,55,37,53,76.5,97,97.5,80,97.5,38,78,95.5,72,59,74,97.5,97.5,52,97.5],
                                                      [82,88,43,87,68,100,82,92,81,90,100,100,85.5,94,51,74,54,95,94,89,95,77,59,94.5,72,72,32,94,90,90,97],
                                                      [86.5,94,94,94.5,89,89,78,72,99,100,99.5,72,78,71.5,76,56,81,79,80,85.5,89,73,73.5,100,93,68,88,92,95,100,80.5]))
    student_homework_scores = {'John':[95,99,85,100,96,97.5,95,89],'Kate':[88,97,86,78,41,81,94,96],'Mike':[89,88,81,85,99,57,69,100],'Danielle':[75,87,67,59,94,92,94,95],'Roger':[97,78,87,93,91,90,67,84]}
    print_homework_averages(student_homework_scores,homework_averages)

   exam_averages = exam_average(remove_zeros([85,80,85,77,83,67,55,75,79,76,72,59,89,87,73,85,78,84,69,75,84,52,85,76,84,90,85,84,80,76,86,75,89,71,64],
                                             [85,89,76,76,85,83,82,83,78,60,79,80,91,86,78,75,93,81,53,80,83,79,73,78,83,77,92,91,77,73,86,77,88,48,73]))
student_exam_scores = {'John':[84,83],'Kate':[93,78],'Mike':[85,73],'Danielle':[100,98],'Roger':[66,88]}
print_exam_averages(student_exam_scores,exam_averages)

def remove_zeros(*all_quizzes):
    result = []
    for each_quiz in all_quizzes:
        updated_quiz_list = []
        for number in each_quiz:
            if number != 0:
                updated_quiz_list.append(number)
        result.append(updated_quiz_list)
    return result

def quiz_average(all_quizzes):
    averages = []
    for each_quiz in all_quizzes:
        each_average = find_average(each_quiz)
        averages.append(each_average)
    return averages

def find_average(array):
    for number in array:
        average = sum(array)/len(array)
    return average

def print_quiz_averages(student_quiz_scores,the_averages):
    count = 0
    for i in the_averages:
        print "The average for quiz",count+1,"is:",i
        count+=1
        if student_quiz_scores[count-1]>the_averages[count-1]:
            print "You did", student_quiz_scores[count-1]-the_averages[count-1], "points higher than the average! :)\n"
        else:
            print "You did", the_averages[count-1]-student_quiz_scores[count-1], "points lower than the average! :(\n"

def homework_average(all_homeworks):
    averages = []
    for each_homework in all_homeworks:
        each_average = find_average(each_homework)
        averages.append(each_average)
    return averages

def print_homework_averages(student_homework_scores,the_averages):
    count = 0
    for i in the_averages:
        print "The average for homework",count+1,"is:",i
        count+=1
        if student_homework_scores[count-1]>the_averages[count-1]:
            print "You did", student_homework_scores[count-1]-the_averages[count-1], "points higher than the average! :)\n"
        else:
            print "You did", the_averages[count-1]-student_homework_scores[count-1], "points lower than the average! :(\n"

def exam_average(all_exams):
    averages = []
    for each_exam in all_exams:
        each_average = find_average(each_exam)
        averages.append(each_average)
    return averages

def print_exam_averages(student_exam_scores,the_averages):
    count = 0
    for i in the_averages:
        print "The average for exam",count+1,"is:",i
        count+=1
        if student_exam_scores[count-1]>the_averages[count-1]:
            print "You did", student_exam_scores[count-1]-the_averages[count-1], "points higher than the average! :)\n"
        else:
            print "You did", the_averages[count-1]-student_exam_scores[count-1], "points lower than the average! :(\n"

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:0)

回应关于有多少代码的评论,但这里有一个你的函数被重写,所以你可以将它推广给你的其他人:

def print_quiz_averages(student_quiz_scores,the_averages):                                                  
  for i, average in enumerate(the_averages):                                                              
    print "The average for quiz",i+1,"is:",i
    for student, student_data in student_quiz_scores.iteritems():                                       
      if student_data[i]>average:
          print("{:s} did {:.2f}% higher than average\n".format( student, student_data[i]-average))
      else:
          print("{:s} did {:.2f}% lower than average\n".format( student, -student_data[i]+average))