A data-structure for 1:1 mappings in python?

前端 未结 8 1502
广开言路
广开言路 2020-12-17 08:17

I have a problem which requires a reversable 1:1 mapping of keys to values.

That means sometimes I want to find the value given a key, but at other times I want to

相关标签:
8条回答
  • 2020-12-17 08:42

    "We can guarantee that either the key or the value (or both) will be an integer"

    That's weirdly written -- "key or the value (or both)" doesn't feel right. Either they're all integers, or they're not all integers.

    It sounds like they're all integers.

    Or, it sounds like you're thinking of replacing the target object with an integer value so you only have one copy referenced by an integer. This is a false economy. Just keep the target object. All Python objects are -- in effect -- references. Very little actual copying gets done.

    Let's pretend that you simply have two integers and can do a lookup on either one of the pair. One way to do this is to use heap queues or the bisect module to maintain ordered lists of integer key-value tuples.

    See http://docs.python.org/library/heapq.html#module-heapq

    See http://docs.python.org/library/bisect.html#module-bisect

    You have one heapq (key,value) tuples. Or, if your underlying object is more complex, the (key,object) tuples.

    You have another heapq (value,key) tuples. Or, if your underlying object is more complex, (otherkey,object) tuples.

    An "insert" becomes two inserts, one to each heapq-structured list.

    A key lookup is in one queue; a value lookup is in the other queue. Do the lookups using bisect(list,item).

    0 讨论(0)
  • 2020-12-17 08:44

    How about using sqlite? Just create a :memory: database with a two-column table. You can even add indexes, then query by either one. Wrap it in a class if it's something you're going to use a lot.

    0 讨论(0)
提交回复
热议问题