Array of dict or lists or ....?

George Sakkis george.sakkis at gmail.com
Wed Oct 8 01:48:30 CEST 2008


On Oct 7, 10:15 pm, Pat <P... at junk.net> wrote:

> Dennis Lee Bieber wrote:
> > On Mon, 06 Oct 2008 19:45:07 -0400, Pat <P... at junk.com> declaimed the
> > following in comp.lang.python:
>
> >> I can't figure out how to set up a Python data structure to read in data
> >> that looks something like this (albeit somewhat simplified and contrived):
>
> >> States
> >>     Counties
> >>       Schools
> >>         Classes
> >>            Max Allowed Students
> >>            Current enrolled Students
>
> >> Nebraska, Wabash, Newville, Math, 20, 0
> >> Nebraska, Wabash, Newville, Gym, 400, 0
> >> Nebraska, Tingo,  Newfille, Gym, 400, 0
> >> Ohio, Dinger, OldSchool, English, 10, 0
>
> >    <snip>
>
> > The structure looks more suited to a database -- maybe SQLite since
> > the interface is supplied with the newer versions of Python (and
> > available for older versions).

Seconded.

> I don't understand why I need a database when it should just be
> a matter of defining the data structure.

Picking an appropriate data structure depends on the kind of
functionality you want to provide. So far you basically described just
one requirement: keep a tally of how many students are in each class
and compare it to the max allowed (and zero). If that's the only kind
of query you want to run against your data, there's no reason to index
separately each state, county, or school; all you care about are
classes. A simple data structure that satisfies perfectly the
requirement could then be:

# mapping of {class-info : (max,enrolled)}

data = {
  ('Nebraska', 'Wabash', 'Newville', 'Math') : (20,  0),
  ('Nebraska', 'Wabash', 'Newville', 'Gym')  : (400, 0),
  ('Nebraska', 'Tingo', 'Newville', 'Gym')   : (400, 0),
  ('Ohio', 'Dinger', 'OldSchool', 'English') : (10,  0),
}

Of course this data structure is pretty bad at answering a query like
"how many classes are there in Nebraska" or "what's the average number
of enrolled students in Newville". The more general information you
might want to get from the data, the more obvious it becomes that you
need a real database.

HTH,
George



More information about the Python-list mailing list