Data structure question

Chris Angelico rosuav at gmail.com
Sun Nov 17 21:14:12 EST 2013


On Mon, Nov 18, 2013 at 1:03 PM, Joseph L. Casale
<jcasale at activenetwerx.com> wrote:
> I have a need for a script to hold several tuples with three values, two text
> strings and a lambda. I need to index the tuple based on either of the two
> strings. Normally a database would be ideal but for a self-contained script
> that's a bit much.
>
> Before I re-invent the wheel, are there any built-in structures that allow for
> this type of use case?

Not entirely sure I understand you, can you post an example?

If what you mean is that you need to locate the function (lambda) when
you know its corresponding strings, a dict will suit you just fine.
Either maintain two dicts for the two separate strings (eg if they're
"name" and "location" and you know whether you're looking up a name or
a location), or maintain a single dict with two entries per tuple (one
for each string). That gives you efficient and clean lookups.

ChrisA



More information about the Python-list mailing list