简体   繁体   中英

Average of a nested dictionary

I have a nested dictionary for a grade-book program that looks like this(this is just an example, it could be any number of students or tests):

workDictionary = {'kevin': {'Test1': 97, 'Test2': 84, 'Test3': 89},
                  ''Bob':{'Test1': 67, 'Test2': 74, 'Test3': 59},
                  'carol':{'Test1': 47, 'Test2': 94, 'Test3': 79},
                  'ted':{'Test1': 67, 'Test2': 64, 'Test3': 99}}

And I want to get the average of the innermost values, for instance:

finalGrade = {}
for k,v in workDictionary.iteritems():
    finalGrade[k] = sum(v)/ float(len(v))

There are other factors however, i'm using pickling and an undefined amount of students and tests. This is the full program:

# Modules
import pickle

def dumpPickle(fileName):
    pickle.dump(workDictionary, open(fileName,'wb'))
    return

def loadUnPickle(fileName):
    global workDictionary
    workDictionary = pickle.load(open(fileName, 'rb'))
    return(workDictionary)

workDictionary = {}
keys = workDictionary.keys()
values = workDictionary.values()

def myMenu():
    mySelect = -1
    while mySelect != 0:
        print("\n1. Open Dictionary File\n"+
              "2. Create/Write to a Dictionary\n"+
              "3. Add a New Student\n"+
              "4. Find a Student's Scores\n"+
              "5. Add a New Student Score\n"+
              "6. Display Dictionary Data\n"+
              "0. Exit\n"
              )
        mySelect = int(input("Enter Menu Number: "))
        if mySelect == 1:
            fileName = input("Enter file name")
            print("\nyour file is now open")
            loadUnPickle(fileName)

        elif mySelect == 2:
            fileName = input("please create a new file.")
            print("\nyour new file is now open")

        elif mySelect == 3:
                newStudent = input("Enter the new student's name")
                firstTest = input("Enter the name of the first test")
                testGrade = input("Enter the new student's first grade")
                addDictionary = {newStudent:{firstTest:testGrade}}
                workDictionary.update(addDictionary)
                print("\n" + newStudent + str(workDictionary[newStudent]))
                dumpPickle(fileName)

        elif mySelect == 4:
            print("\nEnter student name")
            myName = input()
            for name in workDictionary:
                if name == myName:
                    print("\n",workDictionary.get(myName))

        elif mySelect == 5:
            print("\nEnter student name ")
            myName = input()
            print("\nEnter assignment to add or update")
            myValue = input()
            for name in workDictionary:
                if name == myName:
                    newGrade = input("Enter new Grade")
                    workDictionary[name][myValue]= newGrade
                    dumpPickle(fileName)
                    print("\n" + name + str(workDictionary[name]))
        elif mySelect == 6:
            print(workDictionary)
    return

# Main Loop

I want to add another menu selection that takes the average of a certain student and displays it.

This is what i wrote, but you can rewrite it so it would fir better in your program:

def student_avg(student):
    summ = 0
    grades_num = 0
    for test, grade in student.items():
        summ += grade
        # unless you aren't sure that grade would be a int, in which case add exception
        grades_num += 1
    average = summ / grades_num
    return average

average = student_avg(workDict["kevin"])

You can use a Dict Comprehension

from statistics import mean
avg_grades = {name: mean(tests.values()) for (name, tests) in workDictionary.items()}

The result stored in avg_grades will be:

{'Bob': 66.66666666666667,
 'carol': 73.33333333333333,
 'kevin': 90.0,
 'ted': 76.66666666666667}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM