Data structure question

Tim Chase python.list at tim.thechases.com
Sun Nov 17 21:23:09 EST 2013


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








More information about the Python-list mailing list