Re: Python newbie data structures question

2009-10-31 Thread codingJoe
On Oct 29, 2:06 am, Chris Rebert c...@rebertia.com wrote:
 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- 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?


-- 
http://mail.python.org/mailman/listinfo/python-list


Python newbie data structures question

2009-10-29 Thread 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..
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python newbie data structures question

2009-10-29 Thread alex23
On Oct 29, 4:31 pm, codingJoe tracy.monte...@gmail.com wrote:
 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.
-- 
http://mail.python.org/mailman/listinfo/python-list