I've following nested dictionary, where the first number is resource ID (the total number of IDs is greater than 100 000):
dict = {1: {'age':1,'cost':14,'score':0.3},
2: {'age':1,'cost':9,'score':0.5},
...}
I want to add to each resource a sum of costs of resources with lower score than given resource. I can add 'sum_cost' key which is equal to 0 by following code:
for id in adic:
dict[id]['sum_cost'] = 0
It gives me following:
dict = {1: {'age':1,'cost':14,'score':0.3, 'sum_cost':0},
2: {'age':1,'cost':9,'score':0.5,'sum_cost':0},
...}
Now I would like to use ideally for loop (to make the code easily readable) to assign to each sum_cost a value equal of sum of cost of IDs with lower score than the given ID.
Ideal output looks like dictionary where 'sum_cost' of each ID is equal to the cost of IDs with lower score than given ID:
dict = {1: {'age':1,'cost':14,'score':0.3, 'sum_cost':0},
2: {'age':1,'cost':9,'score':0.5,'sum_cost':21},
3: {'age':13,'cost':7,'score':0.4,'sum_cost':14}}
Is there any way how to do it?