简体   繁体   English

如何在Python3中使用sum函数

[英]How to use the sum function in Python3

I'm trying to use the sum function multiple times after one another but I keep getting a error message. 我试图一次又一次地使用sum函数,但始终收到错误消息。 I wrote the following code: 我写了以下代码:

averages["averages_home_goals"] = sum(d['home_goals'] for d in averages.values() if d)
averages["averages_away_goals"] = sum(d['away_goals'] for d in averages.values() if d)
averages["averages_home_conceded"] = sum(d['home_conceded'] for d in averages.values() if d)
averages["averages_away_conceded"] = sum(d['away_conceded'] for d in averages.values() if d)

print(averages)

The averages variable looks like the following: averages变量如下所示:

{
    'Belconnen%20United': {
        'home_goals': 4.0,
        'away_goals': 1.0,
        'home_conceded': 0.0,
        'away_conceded': 2.1666666666666665,
    },
    'Canberra%20FC': {
        'home_goals': 1.75,
        'away_goals': 1.8,
        'home_conceded': 2.0,
        'away_conceded': 2.0,
    },
    # More data of a similar structure
}

When I run this I get the following error: 运行此命令时,出现以下错误:

TypeError: 'float' object is not subscriptable

The weird thing is; 奇怪的是; when I run only one of these 4 lines the code executes fine (I doesn't matter which line). 当我只运行这4行中的一行时,代码可以正常执行(无论哪一行都行)。 I'm getting the error upon executing the averages["averages_away_goals"] = ... statement 我在执行averages["averages_away_goals"] = ...语句时遇到错误

Could anyone help me out? 有人可以帮我吗? Giving variable d another name also doesn't work 给变量d另一个名称也不起作用

The first line will work just fine, but the failing will start on the second line and continues onwards. 第一行可以正常工作,但是失败将从第二行开始并继续。 The reason is that you have included a new entry in the averages dictionary after the first line. 原因是您在第一行之后的averages字典中包含了一个新条目。 Hence your averages dictionary has a new key "averages_home_goals" with a single float value. 因此,您的averages字典具有一个带有单个浮点值的新键"averages_home_goals"

When you execute the second line, you iterate through all the values of the dictionary averages , which now has an additional value of the sum of all home goals in the key "averages_home_goals" , which is going to be a float. 当您执行第二行时,您将遍历字典averages所有值,该averages现在在键"averages_home_goals"具有所有本垒目标和的附加值,该值将是一个浮点数。 And you cannot subscript a float. 而且您不能下标浮点数。 Basically what it means that you are trying to execute an example of a code (on the second line) of these sorts: 基本上,这意味着您试图执行以下类型的代码示例(在第二行):

12.3242["away_goals"]

which will always fail. 这将永远失败。

As a solution I'd suggest to store the results into a new dictionary, fe 作为解决方案,我建议将结果存储到新字典中,例如

res = {}

res["averages_home_goals"] = sum(d['home_goals'] for d in averages.values() if d)
res["averages_away_goals"] = sum(d['away_goals'] for d in averages.values() if d)
res["averages_home_conceded"] = sum(d['home_conceded'] for d in averages.values() if d)
res["averages_away_conceded"] = sum(d['away_conceded'] for d in averages.values() if d)

print(res)

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

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