Andy P
Andy P

Reputation: 111

Inserting values to nested dictionaries

I have some nested dict's in Python and need some help to get my code fully working.

First the code:

data = {}

def insertIntoDataStruct(country, state,job,count,dict):
    if country in dict:
         dict[country][state] = {job: count}
    elif country not in dict:
         dict[country] = {state: {job: count}}
    elif state not in dict[country]:
         dict[country] = {state: {job: count}}
    elif job not in dict[country][state]:
         dict[country][state][job] = count
    else:
         dict[country][state][job] += count


 insertIntoDataStruct("US", "TX", 1234, 1, data)
 insertIntoDataStruct("IN", "KERELA", 1234, 1, data)
 insertIntoDataStruct("IN", "KERELA", 1234, 1, data)
 insertIntoDataStruct("US", "TX", 12345, 1, data)
 insertIntoDataStruct("US", "MI", 1234, 1, data)
 insertIntoDataStruct("IN", "M", 1234, 1, data)

 print data

Currently it is printing this:

{'US': {'MI': {1234: 1}, 'TX': {12345: 1}}, 'IN': {'M': {1234: 1}, 'KERELA': {1234: 1}}}

Its a dictionary that is on the outer level, countries, inner level, states, inner level, job: count

The count is not working correctly as KERELA 1234 should have a 2, and then what it appears to be doing is replacing the latest states with any state thats already in the dictionary. For example TX 1234 does not show up because it was replaced later by TX 12345

Thanks for all help in advance!

Upvotes: 0

Views: 54

Answers (1)

parchment
parchment

Reputation: 4002

Your function need some fixes, try this:

def insertIntoDataStruct(country, state,job,count,dict):
    if country not in dict:
         dict[country] = {state: {job: count}}
    elif state not in dict[country]:
         dict[country][state] = {job: count}
    elif job not in dict[country][state]:
         dict[country][state][job] = count
    else:
         dict[country][state][job] += count

It's probably what you meant originally, if the country doesn't exist, then create it, else if state doesn't exist, etc...

Another way of doing this is with defaultdict:

from collections import defaultdict, Counter
jobsdict = defaultdict(lambda: defaultdict(Counter))
jobsdict['US']['TX']['1234'] += 1

Upvotes: 2

Related Questions