Python newbie data structures question

codingJoe tracy.monteith at gmail.com
Sat Oct 31 20:39:03 CET 2009


On Oct 29, 2:06 am, Chris Rebert <c... at rebertia.com> wrote:
> On Wed, Oct 28, 2009 at 11:31 PM, codingJoe <tracy.monte... at gmail.com> wrote:
> > Hi all!
>
> > I am trying to choose the right data structure to do a value lookup
> > with multiple keys.
>
> > I want to lookup data by:  key, key,{ values }
>
> > My final product should be able to reference this datastructure from
> > within a django template.
>
> > Because my lookup needs only 80 values and will never change, it seems
> > that I could just build some type of data structure or object.   My
> > rudamentary approach is to do lots of looping, but there must be a
> > better way to do this.
>
> > A dictionary object only has (key, value).  The same pair of keys
> > could return multiple values.
> > Tuples might work, but I just don't understand them enough.
>
> > the simple exampe is a quick lookup index the available sport by
> > season that returns one or many value.     The values would be data
> > structures and contain more than just the name, but also things like
> > uniform color, team size.
>
> >             indoors       outdoors
> >            -----------------------------------------------
> > winter   |  bball         |  skiing     |
> >            |                 |  sledding  |
> >            |----------------------------------------------
> >            |                  |  baseball |
> > summer|  raquetball  |  soccer    |
> >            |                  |               |
> >            |
> >            |----------------------------------------------
>
> > Advice?  What data structure should I use?  And a simple newbie
> > example please..
>
> Besides Alex's option of using tuples as dictionary keys:
>
> A. An SQL database
> (http://docs.python.org/library/sqlite3.html#module-sqlite3). Overkill
> for something as simple as your toy example, but in analogous but more
> complicated cases, it's one method to consider.
>
> B. Nested dictionaries. Particularly useful if you want to access the
> data by broader categories (e.g. all summer sports), although it only
> works for one axis of categories (i.e. season or doors-ness); if you
> want to access by the other axis, you have to code it yourself.
> Example:
> sports = {"winter" : {"indoors":["bball"], "outdoors":["skiing",
> "sledding"]}, "summer : {"indoors":["raquetball"],
> "outdoors":["baseball","soccer"]} }
> bball = sports["winter"]["indoors"][0]
> baseball, soccer = sports["summer"]["outdoors"]
> all_summer_sports = sum(sports["summer"].values(), [])
>
> Cheers,
> Chris
> --http://blog.rebertia.com- Hide quoted text -
>
> - Show quoted text -


Nice,  both examples work well on the Python side.  Now I am trying to
print the values from a Django template.  I've nested them in a table
similar to the example above to be rendered in a web page.  How do I
do that?





More information about the Python-list mailing list