Python newbie data structures question

alex23 wuwei23 at gmail.com
Thu Oct 29 08:10:29 CET 2009


On Oct 29, 4:31 pm, codingJoe <tracy.monte... at 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.



More information about the Python-list mailing list