简体   繁体   English

用列表迭代嵌套字典

[英]Iterating nested dictionary with a list

I've been stuck on this for my school work and can't wrap my head around a solution.我的学校工作一直坚持这一点,无法解决解决方案。 I'm a newbie我是新手

Basically I have a list of nested dictionary as follows:基本上我有一个嵌套字典列表如下:

[{'_id': {'month': 12, 'year': 2013}, 'count': 1},
 {'_id': {'month': 4, 'year': 2014}, 'count': 1},  
 {'_id': {'month': 5, 'year': 2014}, 'count': 9},  
 {'_id': {'month': 6, 'year': 2014}, 'count': 16}, 
 {'_id': {'month': 7, 'year': 2014}, 'count': 10}, 
 {'_id': {'month': 8, 'year': 2014}, 'count': 18}, 
 {'_id': {'month': 9, 'year': 2014}, 'count': 5},  
 {'_id': {'month': 10, 'year': 2014}, 'count': 9}, 
 {'_id': {'month': 11, 'year': 2014}, 'count': 4}, 
 {'_id': {'month': 12, 'year': 2014}, 'count': 10},
 {'_id': {'month': 1, 'year': 2015}, 'count': 20}, 
 {'_id': {'month': 2, 'year': 2015}, 'count': 15}, 
 {'_id': {'month': 3, 'year': 2015}, 'count': 3}]

And a list of year range taken from this data以及从此数据中获取的年份范围列表

yearList = [2013,2014,2015]

My objective is for the years that are not full, for example year 2013 has only December, I am trying to append to the list dictionary for months Jan (1) to Nov (11) with "count":0 as below.我的目标是针对未满的年份,例如 2013 年只有 12 月,我正在尝试将 append 到 1 月(1)到 11 月(11)月的列表字典中,“计数”:0 如下所示。

[...
 {'_id': {'month': 09, 'year': 2013}, 'count': 0},
 {'_id': {'month': 10, 'year': 2013}, 'count': 0},
 {'_id': {'month': 11, 'year': 2013}, 'count': 0},
 {'_id': {'month': 12, 'year': 2013}, 'count': 1},
 ...]

My thought process is a little messy.我的思考过程有点混乱。 How do I use my list of years, iterate through the dictionary for that specific year, append the missing months, that do it again for the other years?我如何使用我的年份列表,遍历该特定年份的字典,append 缺失的月份,其他年份再次这样做?

Really really appreciate the assistance!真的非常感谢您的帮助!

im a newbie too and im doing this for my school homework but i will try to explain it properly我也是新手,我这样做是为了我的学校作业,但我会尽量解释清楚

try this:尝试这个:

inputList=[{'_id': {'month': 12, 'year': 2013}, 'count': 1},
 {'_id': {'month': 4, 'year': 2014}, 'count': 1},  
 {'_id': {'month': 5, 'year': 2014}, 'count': 9},  
 {'_id': {'month': 6, 'year': 2014}, 'count': 16}, 
 {'_id': {'month': 7, 'year': 2014}, 'count': 10}, 
 {'_id': {'month': 8, 'year': 2014}, 'count': 18}, 
 {'_id': {'month': 9, 'year': 2014}, 'count': 5},  
 {'_id': {'month': 10, 'year': 2014}, 'count': 9}, 
 {'_id': {'month': 11, 'year': 2014}, 'count': 4}, 
 {'_id': {'month': 12, 'year': 2014}, 'count': 10},
 {'_id': {'month': 1, 'year': 2015}, 'count': 20}, 
 {'_id': {'month': 2, 'year': 2015}, 'count': 15}, 
 {'_id': {'month': 3, 'year': 2015}, 'count': 3}]
yearList=[2013,2014,2015]

for checkYear in yearList:
    for checkMonth in range(1,13):    
        for i in inputList:
            # you want to check if there is a 2013 , 1 in the list or not / if there isnt append it !
            if (i["_id"]["month"]==checkMonth and i["_id"]["year"]==checkYear):
                # use something to show that u have been here 
        # if you never meet the condition above (while looping trough inputList) , append {{'_id': {'month': checkMonth, 'year': checkYear}, 'count': 0} to your inputList

i tried to find a better way but i couldn't, hope it helps我试图找到一个更好的方法,但我不能,希望它有帮助

You can use collections.defaultdict to transform your list of dictionaries into a dictionary of dictionaries.您可以使用collections.defaultdict将您的字典列表转换为字典字典。 That way, you can perform a more efficient padding of years with missing months:这样,您可以在缺少月份的情况下更有效地填充年份:

from collections import defaultdict
d = defaultdict(dict)
data = [{'_id': {'month': 12, 'year': 2013}, 'count': 1}, {'_id': {'month': 4, 'year': 2014}, 'count': 1}, {'_id': {'month': 5, 'year': 2014}, 'count': 9}, {'_id': {'month': 6, 'year': 2014}, 'count': 16}, {'_id': {'month': 7, 'year': 2014}, 'count': 10}, {'_id': {'month': 8, 'year': 2014}, 'count': 18}, {'_id': {'month': 9, 'year': 2014}, 'count': 5}, {'_id': {'month': 10, 'year': 2014}, 'count': 9}, {'_id': {'month': 11, 'year': 2014}, 'count': 4}, {'_id': {'month': 12, 'year': 2014}, 'count': 10}, {'_id': {'month': 1, 'year': 2015}, 'count': 20}, {'_id': {'month': 2, 'year': 2015}, 'count': 15}, {'_id': {'month': 3, 'year': 2015}, 'count': 3}]
for i in data:
   d[i['_id']['year']][i['_id']['month']] = i['count']

r = []
for a, b in d.items():
   for i in range(1, 13):
      r.append({'_id':{'month':i, 'year':a}, 'count':b.get(i, 0)})

Output: Output:

[{'_id': {'month': 1, 'year': 2013}, 'count': 0}, {'_id': {'month': 2, 'year': 2013}, 'count': 0}, {'_id': {'month': 3, 'year': 2013}, 'count': 0}, {'_id': {'month': 4, 'year': 2013}, 'count': 0}, {'_id': {'month': 5, 'year': 2013}, 'count': 0}, {'_id': {'month': 6, 'year': 2013}, 'count': 0}, {'_id': {'month': 7, 'year': 2013}, 'count': 0}, {'_id': {'month': 8, 'year': 2013}, 'count': 0}, {'_id': {'month': 9, 'year': 2013}, 'count': 0}, {'_id': {'month': 10, 'year': 2013}, 'count': 0}, {'_id': {'month': 11, 'year': 2013}, 'count': 0}, {'_id': {'month': 12, 'year': 2013}, 'count': 1}, {'_id': {'month': 1, 'year': 2014}, 'count': 0}, {'_id': {'month': 2, 'year': 2014}, 'count': 0}, {'_id': {'month': 3, 'year': 2014}, 'count': 0}, {'_id': {'month': 4, 'year': 2014}, 'count': 1}, {'_id': {'month': 5, 'year': 2014}, 'count': 9}, {'_id': {'month': 6, 'year': 2014}, 'count': 16}, {'_id': {'month': 7, 'year': 2014}, 'count': 10}, {'_id': {'month': 8, 'year': 2014}, 'count': 18}, {'_id': {'month': 9, 'year': 2014}, 'count': 5}, {'_id': {'month': 10, 'year': 2014}, 'count': 9}, {'_id': {'month': 11, 'year': 2014}, 'count': 4}, {'_id': {'month': 12, 'year': 2014}, 'count': 10}, {'_id': {'month': 1, 'year': 2015}, 'count': 20}, {'_id': {'month': 2, 'year': 2015}, 'count': 15}, {'_id': {'month': 3, 'year': 2015}, 'count': 3}, {'_id': {'month': 4, 'year': 2015}, 'count': 0}, {'_id': {'month': 5, 'year': 2015}, 'count': 0}, {'_id': {'month': 6, 'year': 2015}, 'count': 0}, {'_id': {'month': 7, 'year': 2015}, 'count': 0}, {'_id': {'month': 8, 'year': 2015}, 'count': 0}, {'_id': {'month': 9, 'year': 2015}, 'count': 0}, {'_id': {'month': 10, 'year': 2015}, 'count': 0}, {'_id': {'month': 11, 'year': 2015}, 'count': 0}, {'_id': {'month': 12, 'year': 2015}, 'count': 0}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM