FAQ
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?


Thanks,
jlc

Search Discussions

  • Chris Angelico at Nov 18, 2013 at 2:14 am

    On Mon, Nov 18, 2013 at 1:03 PM, Joseph L. Casale 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
  • Joseph L. Casale at Nov 18, 2013 at 2:43 am

    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.

    Hey guys,
    Basically I have several mappings like the following:


    'string_a', 'string_b', lambda s: ...


    I need to be able to index a mapping (there are ~100) by either string_a or
    string_b and at one point with one of those strings I would pull the lambda
    or the other string.


    The important part is getting the row by either of the two strings. I know I
    could just subclass dict and look it up the hardway with __missing__, just
    wondered if something elegant existed already. I may only know string_b and
    need string_a and the lambda.


    Thanks!
    jlc
  • Tim Chase at Nov 18, 2013 at 2:23 am

    On 2013-11-18 02:03, Joseph L. Casale 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?

    It would help to know how you plan to use it. I *think* what you're
    describing is something like


       data = [
         ("one", "two", lambda: "12"),
         ("three", "four", lambda: "34"),
         ("five", "six", lambda: "56"),
         ("seven", "eight", lambda: "78"),
         ]
       mapping = dict(
         (k, t)
         for t in data
         for k in t[:2]
         )


       for test in (
           "one",
           "two",
           "three",
           "four",
           "five",
           "six",
           "seven",
           "eight"
           ):
         a, b, fn = mapping[test]
         print("%s: %r, %r, %r" % (test, a, b, fn()))


    -tkc
  • Ned Batchelder at Nov 18, 2013 at 2:24 am

    On Sunday, November 17, 2013 9:03:38 PM UTC-5, Joseph L. Casale 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?

    Thanks,
    jlc

    Sounds like you should create a dictionary, and enter each tuple twice, once with each string as a key:


         d = {}
         for t in the_tuples:
             d[t[0]] = d[t[1]] = t


    --Ned.
  • Dave Angel at Nov 18, 2013 at 3:25 am

    On Mon, 18 Nov 2013 02:03:38 +0000, "Joseph L. Casale" 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.

    How about two dictionaries, each containing the same tuples for
    values? If you create a tuple first, then add it to both dicts, you
    won't have any space-wasting duplicates.


    --
    DaveA
  • Joseph L. Casale at Nov 18, 2013 at 3:45 am

    How about two dictionaries, each containing the same tuples for
    values? If you create a tuple first, then add it to both dicts, you
    won't have any space-wasting duplicates.

    Thanks guys.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedNov 18, '13 at 2:03a
activeNov 18, '13 at 3:45a
posts7
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase