Home > Mobile >  How do I calculate an average from averages in a dictionary with multiple values in each key without
How do I calculate an average from averages in a dictionary with multiple values in each key without

Time:11-25

Credits to someone who helped me in a previous post, I am able to find the averages of each class in the dictionary and print out the average of the averages with this code:

def avg(classes):
    average = {}
    for classnames, grades in classes.items():
        average[classnames] = sum(grades) / len(grades)
    return average

classes = {"Spanish II": [100, 99, 100, 98], "US History I": [95, 96, 97, 94]}
averages = avg(classes)
print("Average grades in each class:", averages)

average_of_averages = sum(averages.values())/len(averages)
print("Average of average grades:", average_of_averages)

I just want to know if there is a way to calculate the averages (for both the average grade in each class and the average of those averages totaled together) without using the built-in function sum().

I'm thinking I need a nested for loop: one loop for an average in each class and then another loop to find the average of these averages, but I don't know how to do this because there's multiple values in each key.

CodePudding user response:

why not use pandas instead it is much simpler and faster then using loops. Here's the code you will need to use:

import pandas as pd

classes = {"Spanish II": [100, 99, 100, 98], "US History I": [95, 96, 97, 94]}

df = pd.DataFrame(classes)

avg = df.mean()

avg_of_avg = avg.mean()

CodePudding user response:

you can use numpy.mean() and cast averages.values() to list

import numpy as np
def avg(classes):
    average = {}
    for classnames, grades in classes.items():
        average[classnames] = np.mean(grades)
    return average

classes = {"Spanish II": [100, 99, 100, 98], "US History I": [95, 96, 97, 94]}

averages = avg(classes)
print("Average grades in each class:", averages)

average_of_averages = np.mean(list(averages.values()))
print("Average of average grades:", average_of_averages)

CodePudding user response:

Yes, you can simply add a second loop as follows:

def avg(classes):
    average = {}
    for classnames in classes:
        total = 0
        for grade in classes[classnames]:
            total  = grade        
        average[classnames] = total / len(classes[classnames])
    return average

classes = {"Spanish II": [100, 99, 100, 98], "US History I": [95, 96, 97, 94]}
averages = avg(classes)
print("Average grades in each class:", averages)

total_averages = 0
for classname in averages:
    total_averages  = averages[classname]
average_of_averages = total_averages / len(averages)

print("Average of average grades:", average_of_averages)

If you would like, you can also completely skip the part of calculating each of the individual class averages, and just calculate the entire average at once as follows:

def avg(classes):
    total = 0
    total_length = 0
    for classnames in classes:
        for grade in classes[classnames]:
            total  = grade        
        total_length  = len(classes[classnames])
    return total / total_length

classes = {"Spanish II": [100, 99, 100, 98], "US History I": [95, 96, 97, 94]}
average_of_averages = avg(classes)
print("Average of average grades:", average_of_averages)
  • Related