python - Initializing a variable with a nested dictionary literal -


i unsorted plain list of subsubobjects database (coachdb) , want sort objects in object tree using dictionaries.

my object tree looks this:

object.subobject.subsubobject 

each object type has id unique within object level. idea ids document , insert object in object tree:

oid = doc.getid("object") soid = doc.getid("subobject") ssoid = doc.getid("subsubobject")  objtree[oid][soid][ssoid] = doc 

would work? if yes, how should initialize objtree variable using such nested indexing?

i've tried

objtree = {{{}}} 

but didn't work.

you can use collections.defaultdict:

>>> collections import defaultdict >>> objtree = defaultdict(lambda: defaultdict(dict)) >>> objtree[1][2][3] = 1 >>> objtree defaultdict(<function <lambda> @ 0x99a0ed4>,             {1: defaultdict(<type 'dict'>, {2: {3: 1}})}) >>> objtree[1][2][3] 1 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -