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
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?averagesis, or even just with the values changed.averagesto create your average dict, use another dictionary and the code will work