0

I'm trying to use the sum function multiple times after one another but I keep getting a error message. 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:

{
    '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). I'm getting the error upon executing the averages["averages_away_goals"] = ... statement

Could anyone help me out? Giving variable d another name also doesn't work

6
  • 1
    I would ask "What is averages" but I assume you have a good reason for not showing it or explaining what it is and what you're trying to do. Or am I mistaken? Commented Jun 12, 2019 at 20:52
  • which line is the error ? Commented Jun 12, 2019 at 20:52
  • Looks like you have a dictionary of dictionaries, but the code isn't saying so. Please show us what averages is, or even just with the values changed. Commented Jun 12, 2019 at 20:55
  • @cs95 Added averages Commented Jun 12, 2019 at 20:56
  • 2
    Don't use averages to create your average dict, use another dictionary and the code will work Commented Jun 12, 2019 at 21:03

1 Answer 1

6

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. Hence your averages dictionary has a new key "averages_home_goals" with a single float value.

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. 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, f.e.

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.