On Wed, Oct 28, 2009 at 11:31 PM, codingJoe <tracy.monte...@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 -- http://mail.python.org/mailman/listinfo/python-list