Subdivide nested dictionary, while applying percentage from values in Python -


i want subdivide dictionary, while applying values (which percentages, value of another.

i have sets of data:

{'c-std-b&m-sum': {datetime.date(2015, 5, 20): 0.21484699999999998,                datetime.date(2015, 5, 21): 0.245074,                datetime.date(2015, 5, 22): 0.27874}  {'g-cam-bac-sum': {datetime.date(2015, 5, 20): 0.13294399999999998,                datetime.date(2015, 5, 21): 0.151648,                datetime.date(2015, 5, 22): 0.17248,                datetime.date(2015, 5, 23): 0.195664}  {'g-cam-bac-xs': 0.06, 'g-cam-bac-xxs': 0.01, 'g-cam-bac-xl': 0.11, 'g-cam-bac-s': 0.19, 'g-cam-bac-l': 0.26, 'g-cam-bac-xxl': 0.03, 'g-cam-bac-m': 0.35}  {'c-std-b&m-xl': 0.3, 'c-std-b&m-xxl': 0.11, 'c-std-b&m-s': 0.06, 'c-std-b&m-m': 0.2, 'c-std-b&m-xs': 0, 'c-std-b&m-l': 0.32} 

expected output:

{'c-std-b&m-xl': {datetime.date(2015, 5, 20): 0.21484699999999998*0.3,            datetime.date(2015, 5, 21): 0.245074*0.3,            datetime.date(2015, 5, 22): 0.27874*0.3}  {'c-std-b&m-xxs': {datetime.date(2015, 5, 20): 0.21484699999999998*0.1,            datetime.date(2015, 5, 21): 0.245074*0.1,            datetime.date(2015, 5, 22): 0.27874*0.1}  {'c-std-b&m-xxl': {datetime.date(2015, 5, 20): 0.21484699999999998*0.11,            datetime.date(2015, 5, 21): 0.245074*0.11,            datetime.date(2015, 5, 22): 0.27874*0.11} 

and on, dictionaries. note need result of multiplications on values, not statement, left them on make clearer.

my code far (partial):

def apply_size_distribution(dictionary_with_temporal_distribution):     gown_cap_size = get_size_distribution('g2:g7', 'h2:h7')     cap_medium_demand = gown_cap_size['c-std-b&m-m']     k, v in dictionary_with_temporal_distribution.items():         if k == "c-std-b&m-sum":             dictionary_with_temporal_distribution['c-std-b&m-m'] = dictionary_with_temporal_distribution.pop('c-std-b&m-sum')             k, v in dictionary_with_temporal_distribution['c-std-b&m-m'].items():                 dictionary_with_temporal_distribution["{}".format(k)] = v * cap_medium_demand 

but i'm getting old dictionary changed during iteration. also, using code i'll have copy , paste code each size, changing name of key appropriate size. wondering if there more robust method.

you getting dictionary_with_temporal_distribution changed because in code changing -

dictionary_with_temporal_distribution["{}".format(k)] = v * cap_medium_demand 

instead should consider creating new dictionary @ start , maybe add elements continue processing , @ end return it.

also, instead of hard coding values did - gown_cap_size['c-std-b&m-m'] - k == "c-std-b&m-sum" , should use string.rsplit('-', 1)[0] find out common part - c-std-b&m , use equality.

your code -

def apply_size_distribution(dictionary_with_temporal_distribution):     gown_cap_size = get_size_distribution('g2:g7', 'h2:h7')     retlist = {}     cap_demand in grown_cap_size:         k, v in dictionary_with_temporal_distribution.items():             if k.rsplit('-', 1)[0] == cap_demand.rsplit('-', 1)[0]:                 if cap_demand not in retlist:                     retlist[cap_demand] = {}                 temp = retlist[cap_demand]                 k1, v1 in dictionary_with_temporal_distribution[k].items():                     temp[k1] = v1 * grown_cap_size[cap_demand]     return retlist 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -