Access previous key in a unsorted dictionary

拜拜、爱过 提交于 2020-04-18 05:46:13

问题


I'm trying to get the previous value for a certain calculation in the same loop. As I've highlighted in the code. I'm not sure if that's possible or unsure about how to go about it.

start_year = 2001
year_list = list(range(2000, start_year + 1, 1))
month_day = [('JAN', 31), ('FEB', 28), ('MARCH', 31), ('APRIL', 30), ('MAY', 31), ('JUNE', 30), ('JULY', 31),
            ('AUG', 31), ('SEPT', 30), ('OCT', 31), ('NOV', 30), ('DEC', 31)]

r_no ={2000: {'JAN': 705, 'FEB': 705, 'MARCH': 705, 'APRIL': 705, 'MAY': 705, 'JUNE': 705, 'JULY': 740.25, 'AUG': 740.25, 'SEPT': 740.25, 'OCT': 740.25, 'NOV': 888.3, 'DEC': 888.3}, 2001: {'JAN': 888.3, 'FEB': 888.3, 'MARCH': 740.25, 'APRIL': 740.25, 'MAY': 740.25, 'JUNE': 740.25, 'JULY': 777.26, 'AUG': 777.26, 'SEPT': 777.26, 'OCT': 777.26, 'NOV': 932.71, 'DEC': 932.71}}

d_no = {2000: {'JAN': 0, 'FEB': 52, 'MARCH': 56, 'APRIL': 54, 'MAY': 56, 'JUNE': 54, 'JULY': 62, 'AUG': 62, 'SEPT': 60, 'OCT': 62, 'NOV': 81, 'DEC': 84}, 2001: {'JAN': 84, 'FEB': 78, 'MARCH': 62, 'APRIL': 60, 'MAY': 62, 'JUNE': 60, 'JULY': 68, 'AUG': 68, 'SEPT': 66, 'OCT': 68, 'NOV': 88, 'DEC': 91}}
dist_no = {2000: {'JAN': 36, 'FEB': 36, 'MARCH': 36, 'APRIL': 36, 'MAY': 36, 'JUNE': 36, 'JULY': 40, 'AUG': 40, 'SEPT': 40, 'OCT': 40, 'NOV': 54, 'DEC': 54}, 2001: {'JAN': 54, 'FEB': 54, 'MARCH': 40, 'APRIL': 40, 'MAY': 40, 'JUNE': 40, 'JULY': 44, 'AUG': 44, 'SEPT': 44, 'OCT': 44, 'NOV': 59, 'DEC': 59}}


year_list = list(range(2000, start_year + 1, 1))
for j in year_list:
    price_per_month = {}
    if j == 2000:
        month, days = month_day[0]
        # if month == 'JAN':
        price_per_month_val = 0
        price_per_month[month] = price_per_month_val
        print(price_per_month[month])

        for month_index, (month, days) in enumerate(month_day[1:]):
            price_per_month_val = dist_no[j][month] * days * r_no[j][month]
            # --- >>>> HERE I'm trying to get the previous month value for r_no[j][month-1] 
            price_per_month_val_def = d_no[j][month] * 0.8 * r_no[j][month]
            price_per_month_val = price_per_month_val + price_per_month_val_def
            price_per_month[month] = price_per_month_val

    elif j >= 2001:
        month_def = {}
        month, days = month_day[0]
        price_per_month_val = dist_no[j][month] * days * r_no[j - 1]['DEC']
        price_per_month[month] = price_per_month_val

    for month_index, (month, days) in enumerate(month_day[1:]):
        price_per_month_val = dist_no[j][month] * days * r_no[j][month]
        # --->>>> HERE I'm trying to get the previous month value for r_no[j][month-1] 
        price_per_month_val_def = d_no[j][month] * 0.8 * r_no[j][month]
        price_per_month_val = price_per_month_val + price_per_month_val_def
        price_per_month[month] = price_per_month_val

print(price_per_month)

来源:https://stackoverflow.com/questions/61264087/access-previous-key-in-a-unsorted-dictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!