0

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!

3
  • 1
    because in the second call with KERELA, the first condition if country in dict: is true. Commented Oct 17, 2014 at 15:19
  • 1
    Your code is really strange. First you check if an item is in the dict, and than your checking whether its not in the dict. With other words, that will be the only two scenarios working. Commented Oct 17, 2014 at 15:20
  • @AndyP I have edited my answer to add an alternative way of doing this. Commented Oct 17, 2014 at 15:36

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

(By the way you can took a look to collections.Counter for the job counter)
Hmm, actually this can be implemented as some defaultdicts. Thanks for reminding me of collections @Cld

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.