Home > Back-end >  Iterating nested dictionary with a list
Iterating nested dictionary with a list

Time:11-21

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.

[...
 {'_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?

Really really appreciate the assistance!

CodePudding user response:

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

CodePudding user response:

You can use collections.defaultdict to transform your list of dictionaries into a dictionary of dictionaries. 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:

[{'_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}]
  • Related