Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 循环通过dict';s内部列表,添加缺少的月份_Python_Python 3.x_Dictionary - Fatal编程技术网

Python 循环通过dict';s内部列表,添加缺少的月份

Python 循环通过dict';s内部列表,添加缺少的月份,python,python-3.x,dictionary,Python,Python 3.x,Dictionary,我一直在苦苦思索如何循环浏览列表,将丢失的几个月的0添加到原始词典中。我想从日历中创建一个月的列表,循环每个月,然后在我的数据中循环每个月……但我不太明白如何在缺少一个月时以正确的顺序更新字典 import calendar my_dict = {'Green Car': [('January', 340), ('February', 2589), ('March', 12750), ('April', 114470),

我一直在苦苦思索如何循环浏览列表,将丢失的几个月的0添加到原始词典中。我想从
日历
中创建一个月的列表,循环每个月,然后在我的数据中循环每个月……但我不太明白如何在缺少一个月时以正确的顺序更新字典

import calendar

my_dict = {'Green Car': 
        [('January', 340), 
        ('February', 2589), 
        ('March', 12750), 
        ('April', 114470), 
        ('July', 4935),
        ('August', 1632),
        ('September', 61),
        ('December', 3409)], 
    'Red Truck': 
        [('January', 2325185), 
        ('February', 209794), 
        ('March', 201874),
        ('April', 19291),
        ('May', 18705), 
        ('July', 22697), 
        ('August', 22796)], 
    'Police Car': 
        [('January', 2037), 
        ('February', 2620), 
        ('March', 1480), 
        ('April', 15630), 
        ('July', 40693), 
        ('August', 2329)], 
    'Zamboni': 
        [('January', 256), 
        ('February', 426690), 
        ('March', 589), 
        ('April', 4740), 
        ('May', 880), 
        ('July', 1016), 
        ('August', 106), 
        ('September', 539), 
        ('October', 598), 
        ('November', 539), 
        ('December', 470)], 
    'Witch Broom': 
        [('February', 350),
        ('March', 3520), 
        ('October', 2703), 
        ('November', 2221), 
        ('December', 664)]
    }


def fill_months(reported_months):
    const_months = list(calendar.month_name)
    x = 0
    print("Looking for months in", reported_months) 
    # print(const_months)
    for const_month in const_months:
        for month in reported_months:
            if const_month != month[0] and len(const_month) > 0:
                print(const_month, month[0])
                print("You don't have", const_month, "in the months group:", reported_months)


def main():
    for commod, months in my_dict.items():
        # print(commod)
        # print(commod, months)
        fill_months(months)


if __name__ == '__main__':
    main()
对于每个键(“绿色汽车”、“红色卡车”等),我想循环并添加一个缺失的moth,其值为
0
。因此,最终的“绿色汽车”将是:

my_dict = {'Green Car': 
        [('January', 340), 
        ('February', 2589), 
        ('March', 12750), 
        ('April', 114470),
        ('May', 0),
        ('June', 0),
        ('July', 4935),
        ('August', 1632),
        ('September', 61),
        ('October', 0),
        ('November', 0),
        ('December', 3409)], 

我已经取得了一些进展,但逻辑似乎有些混乱:

def fill_months(reported_months):
    const_months = list(calendar.month_name)
    x = 0
    temp_months = []
    for i in reported_months:
        temp_months.append(i[0])
    print("Looking for months in", reported_months) 
    # print(const_months)
    for const_month in const_months:
        if len(const_month) > 0:
            if const_month not in temp_months:
                reported_months.insert(x-1, (const_month, 0))
        x += 1
    print(reported_months)

您可以使用
设置
差异来获取您的
报告月份
中缺少的月份,然后将它们与0一起添加到列表中

带bool的过滤器用于从列表中删除空字符串

import calendar


my_dict = {
'Green Car':
        [('January', 340),
        ('February', 2589),
        ('March', 12750),
        ('April', 114470),
        ('July', 4935),
        ('August', 1632),
        ('September', 61),
        ('December', 3409)],
    'Red Truck':
        [('January', 2325185),
        ('February', 209794),
        ('March', 201874),
        ('April', 19291),
        ('May', 18705),
        ('July', 22697),
        ('August', 22796)],
    'Police Car':
        [('January', 2037),
        ('February', 2620),
        ('March', 1480),
        ('April', 15630),
        ('July', 40693),
        ('August', 2329)],
    'Zamboni':
        [('January', 256),
        ('February', 426690),
        ('March', 589),
        ('April', 4740),
        ('May', 880),
        ('July', 1016),
        ('August', 106),
        ('September', 539),
        ('October', 598),
        ('November', 539),
        ('December', 470)],
    'Witch Broom':
        [('February', 350),
        ('March', 3520),
        ('October', 2703),
        ('November', 2221),
        ('December', 664)]
    }


def filter_month(x, months):
    """filters the month when month[i] == x
    """
    return [i[1] for i in months if i[0] == x][0]

def fill_months(reported_months):
    const_months = list(filter(bool, calendar.month_name))
    my_months = set(map(lambda x: x[0], reported_months))
    missing_months = set(const_months) - my_months

    for month in missing_months:
        reported_months.append((month, 0))

    # arranging
    arranged = list(map(lambda x: (x, filter_month(x, reported_months)), const_months))
    for i in range(len(reported_months)):
        reported_months[i] = arranged[i]

def main():
    for commod, months in my_dict.items():
        fill_months(months)

if __name__ == '__main__':
    main()

这会将月份添加到列表中,然后对月份列表进行排序

# remove the leading empty '' from calendar.month_name list
monthnames = [x for x in calendar.month_name if x != ''] 

# scan dicts for missing monts, if found, append tuple (monthname,0) to list
for part in my_dict:
    print(part)
    for month in monthnames:
        if not month in [x[0] for x in my_dict[part]]:
            my_dict[part].append( (month,0) ) 

# modified https://stackoverflow.com/questions/10695139/sort-a-list-of-tuples-by-2nd-item-integer-value
# interrate through dict, assign sorted dict, use indexlookup into monthlist for sorting
for part in my_dict:
    my_dict[part] = sorted(my_dict[part], key=lambda x: monthnames.index(x[0]))

print(my_dict)
输出:

{'Red Truck'
    : [('January', 2325185)
        ,( 'February', 209794)
        ,( 'March', 201874)
        ,( 'April', 19291)
        ,( 'May', 18705)
        ,( 'June', 0)
        ,( 'July', 22697)
        ,( 'August', 22796)
        ,( 'September', 0)
        ,( 'October', 0)
        ,( 'November', 0)
        ,( 'December', 0)], 
'Witch Broom'
    : [('January', 0)
        ,( 'February', 350)
        ,( 'March', 3520)
        ,( 'April', 0)
        ,( 'May', 0)
        ,( 'June', 0)
        ,( 'July', 0)
        ,( 'August', 0)
        ,( 'September', 0)
        ,( 'October', 2703)
        ,( 'November', 2221)
        ,( 'December', 664)], 
'Police Car'
    : [('January', 2037)
        ,( 'February', 2620)
        ,( 'March', 1480)
        ,( 'April', 15630)
        ,( 'May', 0)
        ,( 'June', 0)
        ,( 'July', 40693)
        ,( 'August', 2329)
        ,( 'September', 0)
        ,( 'October', 0)
        ,( 'November', 0)
        ,( 'December', 0)], 
'Green Car'
    : [('January', 340)
        ,( 'February', 2589)
        ,( 'March', 12750)
        ,( 'April', 114470)
        ,( 'May', 0)
        ,( 'June', 0)
        ,( 'July', 4935)
        ,( 'August', 1632)
        ,( 'September', 61)
        ,( 'October', 0)
        ,( 'November', 0)
        ,( 'December', 3409)], 
'Zamboni'
    : [('January', 256)
        ,( 'February', 426690)
        ,( 'March', 589)
        ,( 'April', 4740)
        ,( 'May', 880)
        ,( 'June', 0)
        ,( 'July', 1016)
        ,( 'August', 106)
        ,( 'September', 539)
        ,( 'October', 598)
        ,( 'November', 539)
        ,( 'December', 470)]}

添加缺少月份的逻辑需要检查的是,如果月份在索引上与您拥有的某些月份名称列表不匹配,
months
。如果它们确实匹配,则递增,并循环,直到涵盖
月份的每个成员

months = ['January', 'February', 'March', 'April', 'May', 'June', 
          'July', 'August', 'September', 'October', 'November', 'December']

def add_missing_months(vehicle_d):    
    for vehicle in vehicle_d:
        ind = 0
        month_l = vehicle_d[vehicle]
        while ind < len(months):
            if ind >= len(month_l) or month_l[ind][0] != months[ind]:
                month_l.insert(ind, (months[ind], 0))
            else:
                ind += 1
    return vehicle_d
months=[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”,
“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”]
def添加缺少的月份(车辆d):
对于车辆中的车辆:
ind=0
月份=车辆日[车辆]
当ind=len(month_l)或month_l[ind][0]!=月份[ind]:
插入月份(ind,(月份[ind],0))
其他:
ind+=1
返回车辆

这是有效的,尽管它会将月份添加到末尾。是否有办法保持排序并按时间顺序插入月份?