简体   繁体   English

如何从字典中的平均值计算平均值

[英]How to calculate an average from average values in a dictionary

I wrote a function to have my dictionary, classes, as my parameter:我写了一个 function 有我的字典,类,作为我的参数:

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?它计算列表中每个 class 的平均值,但是我将如何计算字典中所有类的平均值在 function 中执行它? The two averages are 99.25 and 95.5, as shown in the output:两个平均值分别为 99.25 和 95.5,如 output 所示:

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

So the average of these averages would be 97.375.所以这些平均值的平均值是 97.375。 How would I get my function to print that?我怎样才能让我的 function 打印出来?

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

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.列表推导比 for 循环更快,通常更容易阅读。

sum(list_comprehension)

returns the sum of the values in the list comprehension.返回列表推导中的值的总和。

sum/len总和/长度

returns the average.返回平均值。

After populating your average dict, take the average of its values.填充您的average字典后,取其值的平均值。

def avg(classes):    # fixing the argument name
    average = {}
    for classnames, grades in classes.items():
        average[classnames] = sum(grades) / len(grades)
    average['Average'] = sum(average.values()) / len(average)
    return average   # better to return than print the value

To get a list of the values in a python dictionary, you can use the method average.values().要获取 python 字典中的值列表,可以使用方法 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)

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.您可以使用.values新创建的字典的值,然后应用您之前应用的相同逻辑来获得平均值。

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

you can do everything in one go and have it organized in a dict:您可以在一个 go 中完成所有操作,并将其组织在一个字典中:

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 output

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

The logic to calculate the average is (value1 + value2 + value3 +...) / (number of values).计算平均值的逻辑是(value1 + value2 + value3 +...)/(值的数量)。

For example, [35, 24, 16] can be caculated as (35 + 24 + 16) / 3 .例如, [35, 24, 16]可以计算为(35 + 24 + 16) / 3 So 35 + 24 + 16 = 75, and 75 / 3 is 25.所以 35 + 24 + 16 = 75,而 75 / 3 是 25。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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