我一直在努力学习如何遍历列表,将缺少的几个月的0添加回原始字典中。我正在考虑创建一个月份列表,从calendar
开始循环遍历每个月,然后在我的数据中循环每个月......但是当丢失时,无法弄清楚如何更新字典月份正确。
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()
对于每个键(“绿色汽车”,“红色卡车”等),我想循环并添加一个值为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 :(得分:1)
这会将月份添加到您的列表中,然后对月份列表进行排序
# 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)]}
答案 1 :(得分:1)
所有添加缺失月份的逻辑必须检查的是,如果月份与索引匹配到您拥有的某些月份名称months
。如果匹配则递增,并循环直到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
答案 2 :(得分:0)
您可以使用sets
差异来确定reported_month
中缺少的月份,然后将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()