File: other-functionality.rst

package info (click to toggle)
bidict 0.23.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,148 kB
  • sloc: python: 1,603; makefile: 157; sh: 114; javascript: 25; xml: 9
file content (37 lines) | stat: -rw-r--r-- 903 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Other Functionality
===================

:func:`bidict.inverted`
-----------------------

Bidict provides the :class:`~bidict.inverted` iterator
to help you get inverse items from various types of objects.

Pass in a mapping to get the inverse mapping:

.. doctest::

   >>> from bidict import inverted
   >>> it = inverted({1: 'one'})
   >>> {k: v for (k, v) in it}
   {'one': 1}

...an iterable of pairs to get the pairs' inverses:

.. doctest::

   >>> list(inverted([(1, 'one'), (2, 'two')]))
   [('one', 1), ('two', 2)]
   >>> list(inverted((i*i, i) for i in range(2, 5)))
   [(2, 4), (3, 9), (4, 16)]

...or any object implementing an ``__inverted__`` method,
which objects that already know their own inverses (such as bidicts)
can implement themselves:

.. doctest::

   >>> dict(inverted(bidict({1: 'one'})))
   {'one': 1}
   >>> list(inverted(OrderedBidict({2: 4, 3: 9})))
   [(4, 2), (9, 3)]