question about dictionary type..

Padraig Brady Padraig at Linux.ie
Fri Sep 13 12:33:23 EDT 2002


eugene kim wrote:
> hi..
> 
> i'd like to have a dictionary which looks like
> table = { category1 : { category2 : { category3 : 'garbage value' } } }
> 
> category1 has many category2
> category2 has many category3
> 
> i expected
> table[ 'computer' ][ 'language' ][ 'python' ] = 0
> to create {computer : { language : { python :0 }}}
> table[ 'computer' ][ 'language' ][ 'java' ] = 0
> to becomes { 'computer' : { 'language' : { 'python' :0 , 'java' :0 }}}
> 
> but python doesn't allow me to do this..
> can anyone help me?
> 

It only creates 1 level at a time
because ypur not acutally assigning
to table[ 'computer' ] or table[ 'computer' ][ 'language' ],
you're only referencing them.

I guess you have to do something like:
table.setdefault('computer',{}).setdefault('language',{})['python']=0

Pádraig.




More information about the Python-list mailing list