Home > Blockchain >  How to calculate an average from average values in a dictionary
How to calculate an average from average values in a dictionary

Time:11-23

I wrote a function to have my dictionary, classes, as my parameter:

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

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

It calculates the average of each class from the list, but how would I calculate the average of the averages from all the classes in the dictionary doing it in the function? The two averages are 99.25 and 95.5, as shown in the output:

{'Spanish II': 99.25, 'US History I': 95.5}

So the average of these averages would be 97.375. How would I get my function to print that?

CodePudding user response:

The solution looks very similar to what you've already got:

def average_of_averages(averages):
    output = 0
    for classname, average in averages.items():
        output  = average
    output /= len(averages)
    return output

CodePudding user response:

To get a list of the values in a python dictionary, you can use the method average.values(). Using that, you could write this code to find the average grade of all the classes:

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

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

CodePudding user response:

You can take the values of your newly created dictionary using .values and then apply the same logic you applied earlier to get an average of averages.

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)
#{'Spanish II': 99.25, 'US History I': 95.5}

average_of_averages = sum(averages.values())/len(averages)
#97.375

CodePudding user response:

You're slightly complicating the problem.

All you need is a one liner to solve this (look up list comprehensions)

classes = {'Spanish II': 99.25, 'US History I': 95.5}

def average(class_dict):
    return sum([value for value in class_dict.values()])/len(class_dict)

Let's break the code down to make it more manageable:

[value for value in class_dict.values()]

returns a list of all values in the dictionary. List comprehensions are faster and generally easier to read than a for loop.

sum(list_comprehension)

returns the sum of the values in the list comprehension.

sum/len

returns the average.

CodePudding user response:

you can do everything in one go and have it organized in a dict:

def avg(classes):
    averages = { k: sum(v)/len(v) for k,v in classes.items() } #creates a dict with average of each subject
    return averages | {"course": sum(vals:=averages.values())/len(vals) } #adds and returns the total average

output

{'Spanish II': 99.25, 'US History I': 95.5, 'course': 97.375}

CodePudding user response:

The logic to calculate the average is (value1 value2 value3 ...) / (number of values).

For example, [35, 24, 16] can be caculated as (35 24 16) / 3. So 35 24 16 = 75, and 75 / 3 is 25.

  • Related