Playing with dictionaries

David Eppstein eppstein at ics.uci.edu
Mon Sep 22 20:17:28 CEST 2003


In article <eppstein-2C8472.10575222092003 at news.service.uci.edu>,
 David Eppstein <eppstein at ics.uci.edu> wrote:

> def makehierarchy(dataset,prefix=''):
>    for key in dataset:
>       if dataset[key] is None:
>          dataset[key] = prefix + key
>       elif isinstance(dataset[key], dict):
>          makehierarchy(dataset[key], prefix + key + ".")
>       else:
>          raise ValueError, "Unexpected data type in makehierarchy"
> 
> >>> makehierarchy(dataset,'dataset')

Sorry, cut-and-paste error here -- updated the live code and forgot to 
update the copy in my posting.  That should be makehierarchy(dataset).

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

-- 
David Eppstein                      http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science




More information about the Python-list mailing list