bijection¶
Bijections are functions that map keys to unique values, ie. one-to-one, onto functions. See: https://en.wikipedia.org/wiki/Bijection
bijection maintains the inverse mapping on bijection.inverse which is itself an instance of bijection.
Examples¶
>>> from collections_extended import bijection
>>> bij = bijection({'a': 1, 'b': 2, 'c': 3})
>>> bij.inverse[2]
'b'
>>> bij['a'] = 2
>>> bij == bijection({'a': 2, 'c': 3})
True
>>> bij.inverse[1] = 'a'
>>> bij == bijection({'a': 1, 'c': 3})
True
>>> bij.inverse.inverse is bij
True
API¶
-
class
collections_extended.
bijection
(iterable=None, **kwarg)[source]¶ Bases:
collections.abc.MutableMapping
A one-to-one onto mapping, a dict with unique values.
-
__init__
(iterable=None, **kwarg)[source]¶ Create a bijection from an iterable.
Matches dict.__init__.
-
property
inverse
¶ Return the inverse of this bijection.
-