I have two dicts like this,
past =
{
'500188':
{
2: {'S': 16.97011741552128, 'C': 16.97011741552128},
3: {'S': -41.264072314989576, 'C': 'ERROR: reported_eps value not found for the year 2012.'},
4: {'S': -40.45410186823402, 'C': 'ERROR: reported_eps value not found for the year 2012.'}
},
'524715':
{
2: {'S': 46.21665549733925, 'C': 38.67504905630727},
3: {'S': -32.729615295373385, 'C': -34.21172523465267},
4: {'S': -22.25028773515787, 'C': -36.041635048402}
},
'513683':
{
2: {'S': 6.319158390481139, 'C': 6.319158390481139},
3: {'S': 19.81072942574542, 'C': 19.81072942574542},
4: {'S': 6.367182731764687, 'C': 'ERROR: reported_eps value not found for the year 2008.'}
}
}
future =
{
'500188':
{
2: {'S': 16.97011741552128, 'C': 16.97011741552128},
3: {'S': -41.264072314989576, 'C': 'ERROR: reported_eps value not found for the year 2012.'},
4: {'S': -40.45410186823402, 'C': 'ERROR: reported_eps value not found for the year 2012.'}
},
'524715':
{
2: {'S': 46.21665549733925, 'C': 38.67504905630727},
3: {'S': -32.729615295373385, 'C': -34.21172523465267},
4: {'S': -22.25028773515787, 'C': -36.041635048402}
}
}
to add them I have done this,
def _float(value):
try:
return float(value)
except ValueError:
return 0
print {key:
{
year: {
_type:
(_float(past.get(key, {}).get(year, {}).get(_type, 0)) + _float(future.get(key, {}).get(year, {}).get(_type, 0)))/2 for _type in ['S', 'C']
}for year in [4,3,2] #Second Loop
}for key in set(past.keys()+future.keys()) #First Loop
}
got the desired output,
{
'500188':
{
2: {'S': 16.97011741552128, 'C': 16.97011741552128},
3: {'S': -41.264072314989576, 'C': 0},
4: {'S': -40.45410186823402, 'C': 0}
},
'513683':
{
2: {'S': 3.1595791952405694, 'C': 3.1595791952405694},
3: {'S': 9.90536471287271, 'C': 9.90536471287271},
4: {'S': 3.1835913658823434, 'C': 0.0}
},
'524715':
{
2: {'S': 46.21665549733925, 'C': 38.67504905630727},
3: {'S': -32.729615295373385, 'C': -34.21172523465267},
4: {'S': -22.25028773515787, 'C': -36.041635048402}
}
}
But, there should be some better solution than this, I googled and found this similar questions,
"Adding" Dictionaries in Python?
python dict.add_by_value(dict_2)?
but the values are in top level, but in my case the value is not in top level and I have to do a type check and have to calculate mean. what is the best way to solve this.
(Python version 2.7)