Complex Nested Dictionaries

Scott David Daniels Scott.Daniels at Acm.Org
Sat Feb 21 13:31:03 EST 2004


T. Earle wrote:
> Russell,
> 
> 
>>If you really only want to look up data by headline, then a dictionary
>>of dictionaries or nested lists or some other kind of collection is easy
>>and should suffice. For instance:
>>warndict["High Wind Warning"] = (
>>    (time1, {
>>        state1: (zone1, zone2, zone3),
>>        state2: (zone1, zone3),
>>    }),
>>    (time2, {...}),
>>)
> This definitely seems to be the structure I've been looking for or at least
> have in mind.  Since I'm no expert, could offer some code examples on how to
> create this structure on the fly?
For something very much (but not quite) like the above:

warndict['High Wind Warning'] = {
     time1: {
         state1: [zone1, zone2, zone3],
         state2: [zone1, zone3]},
     time2: {...},
     ...}

can be built with something like:
warndict = {}
for headline, time, state, zone in somesource:
     timedict = warndict.setdefault(headline, {})
     statedict = timedict.setdefault(time, {})
     stateentry = statedict.setdefault(state, [])
     stateentry.append(zone)


> My first inclination was to go with a database; however, I thought about it
> and concluded there may be too much variability each time the program is
> executed.  For example, there will be times when there are no headlines;
> other times, there will be numerous headlines.  Because of this variability,
> the database would have to be created from scratch each time the program is
> ran.  As a result, would a database still be the right choice?
It really depends on the volume of data and the kinds of searches.
Anything under a thousand or so entries will be searchable by simple
brute force in reasonable time, so internal data structures may well
be the way to go.

-- 
-Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list