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!
if country in dict:is true.