Python newbie data structures question

C

codingJoe

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..
 
A

alex23

I am trying to choose the right data structure to do a value lookup
with multiple keys.

Hey Joe,

Is something like this what you're after?
from collections import defaultdict
sports = defaultdict(list)
sports['winter','indoors'].append('bball')
sports['winter','outdoors'].append('skiing')
sports['winter','outdoors'].append('sledding')
sports['summer','indoors'].append('raquetball')
sports['summer','outdoors'].append('baseball')
sports['winter','outdoors'] ['skiing', 'sledding']
sports['outdoors','winter'] # note that the order of the items is important
[]

(defaultdict is available in Python 2.5+)

Tuples are immutable (unmodifiable) objects, which allows you to use
them as dictionary keys. They're really not scary at all ;)

Hope this helps.
 
C

Chris Rebert

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
 
C

codingJoe

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top