Complex Nested Dictionaries

omission9 omission9 at
Fri Feb 20 02:29:46 CET 2004

T. Earle wrote:

> To list,
> I'm trying to figure out the best approach to the following problem:
> I have four variables:
>    1) headlines
>    2) times
>    3) states
>    4) zones
> At this time, I'm thinking of creating a dictionary, headlinesDB, that
> stores different headlines and their associated time(s), state(s), and
> zone(s). The complexity is that each headline can have one or more times,
> one or more states, and one or more zones.  However, there can only be 1
> zone per time, and 1 zone per state.  What is the best way to tackle this
> particular problem?
> Here's an example of the complexity:
> Let's say we have a "High Wind Warning" for our headline or hazard.  In
> addition, there are currently two "High Wind Warnings" in effect.  The first
> goes from Tonight through Friday morning (i.e., I'll probably store the
> begin/end times in seconds from 1/1/1970).  It affects three counties all in
> the state of Oregon: ORZ047, ORZ048, and ORZ049.  The second High Wind
> Warning is in effect from Friday at Noon through Friday evening.  It affects
> two counties in two separate states:  ORZ044 in Oregon and WAZ028 in
> Washington.  Here's the flow chart:
> High Wind Warning --> time1 --> state1 --> zone1, zone2, zone3
>                                |
>                                --> time2 --> state1 --> zone4
>                                               --> state2 --> zone5
> Keep in mind, each headline or hazard can have multiple times.  Each time
> will have one or more states with each state containing one or more zones.
> Is there a better way than a dictionary.  As mentioned above, the headline
> or hazard is the key I'll be extracting all the information from.
> Thanks in advance,
> Tom

I'd recommend the mx.DateTime package for storing the times instead of 
seconds. That module includes many useful functions may be need so give 
it a look.
Secondly, although I am not 100% sure about the stated problem I would 
recommend that instead of nested dictionaries you use a tuple as a 
key,say, headlines[(time,state,zone)]=someValue
 From what you say above it would seem that this would create a unique 
key for all the mentioned situations.

More information about the Python-list mailing list