question about dictionary type..

Mark McEahern marklists at mceahern.com
Fri Sep 13 12:18:40 EDT 2002


[eugene kim]
> 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?

In this case, the problem is obvious, but rather than "python doesn't allow
me to do this?" it's generally more helpful to provide exact tracebacks or
more explicit information about what's not working.

The problem?  You're not telling Python to create the sub dictionaries...

$ python
Python 2.2.1 (#1, Jun 25 2002, 10:55:46)
[GCC 2.95.3-5 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> t = {}
>>> t['computer'] = {}
>>> t['computer']['language'] = {}
>>> t
{'computer': {'language': {}}}
>>>

etc.

// m





More information about the Python-list mailing list