Playing with dictionaries

Roberto A. F. De Almeida roberto at dealmeida.net
Mon Sep 22 19:07:23 CEST 2003


Hi,

Suppose I have a dictionary containg nested dictionaries. Something
like this:

>>> pprint.pprint(dataset)
{'casts': {'experimenter': None,
           'location': {'latitude': None,
                        'longitude': None},
           'time': None,
           'xbt': {'depth': None,
                   'temperature': None}},
 'catalog_number': None,
 'z': {'array': {'z': None},
       'maps': {'lat': None,
                'lon': None}}}

I want to assign to the values in the dictionary the hierarchy of keys
to it. For example:

>>> dataset['casts']['experimenter'] = 'casts.experimenter'
>>> dataset['z']['array']['z'] = 'z.array.z'

Of course I would like to do this automatically, independent of the
structure of the dictionary. Is there an easy way to do it?

Thanks,

Roberto




More information about the Python-list mailing list