This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: bijective invertible map
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.11
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: Dennis Sweeney, jon.balloch, rhettinger
Priority: normal Keywords:

Created on 2022-03-29 22:15 by jon.balloch, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg416304 - (view) Author: Jonathan Balloch (jon.balloch) Date: 2022-03-29 22:15
It would be powerful to have a native implementation of a bijective map (e.g. a dictionary that hashed only one-to-one, but as a result either the "key" or the "value" could do lookup in O(1) time with the only overhead being the additional memory overhead of O(2N) as many references. 

Calling the object type "bimap", this could be easily implemented by simply having a call to bimap.inverse[value]=key, where the 'inverse' keyword is a reference table to the value-->key references. 

This is an important enhancement because currently the most efficient way to implement this is python is to, either: (1) make a custom object type that keeps two dictionaries, one that maps v->k and one that maps k->v, which takes twice as much memory, or (2) an object that has a custom "inverse" lookup call, which will be slower than O(1). In both cases there is no implicit enforcement of values being unique (necessary for a bijection). 

This should be added to the `collections` library as it will fit well along side other unique hashed collections such as "OrderedDict"

This will be beneficial to the community because transformations between semantic spaces (e.g. things that cannot be done in NumPy or similar) could be much more efficient and have cleaner, easier to read code if bijection maps were native and used one structure instead of two dictionaries.
msg416307 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python committer) Date: 2022-03-29 23:06
see also https://bugs.python.org/issue44931
msg416312 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2022-03-30 00:44
This is indeed a duplicate.  If needed just use one of implementations on PyPI https://pypi.org/project/bidict/
msg416319 - (view) Author: Jonathan Balloch (jon.balloch) Date: 2022-03-30 03:06
thank you!!

On Tue, Mar 29, 2022 at 8:44 PM Raymond Hettinger <report@bugs.python.org>
wrote:

>
> Raymond Hettinger <raymond.hettinger@gmail.com> added the comment:
>
> This is indeed a duplicate.  If needed just use one of implementations on
> PyPI https://pypi.org/project/bidict/
>
> ----------
> nosy: +rhettinger
> resolution:  -> duplicate
> stage:  -> resolved
> status: open -> closed
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue47157>
> _______________________________________
>
History
Date User Action Args
2022-04-11 14:59:57adminsetgithub: 91313
2022-03-30 03:06:28jon.ballochsetmessages: + msg416319
2022-03-30 00:44:14rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg416312

resolution: duplicate
stage: resolved
2022-03-29 23:06:45Dennis Sweeneysetnosy: + Dennis Sweeney
messages: + msg416307
2022-03-29 22:15:27jon.ballochcreate