python bijection

Joshua Bronson jabronson at gmail.com
Tue Dec 1 13:31:34 EST 2009


On Nov 27, 1:12 pm, Francis Carr <coldtortuga at gmail.com> wrote:
> I was really inspired by this discussion thread! :-)
>
> After much tinkering, I think I have a simpler solution.  Just make
> the inverse mapping accessible via an attribute, -AND- bind the
> inverse of -THAT- mapping back to the original.  The result is a
> python dict with NO NEW METHODS except this inverse-mapping
> attribute.  I have posted it on code.activestate.com as <a
> href="http://code.activestate.com/recipes/576968/">Recipe 576968:
> Flipdict -- python dict that also maintains a one-to-one inverse
> mapping</a>
>
>  -- F. Carr

I noticed the phonebook example in your ActiveState recipe and thought
you might consider changing it to something like husbands to wives,
since the names-to-phone-numbers relation is many-to-many. The built-
in htmlentifydefs module provides fodder for a real-world example: It
maintains name2codepoint and codepoint2name as two separate dicts.

Raymond, do you think there might be any future in including a built-
in bidict data structure in Python? At least there's one built-in
module that might benefit from it.

P.S. I moved bidict to its own repo at http://bitbucket.org/jab/bidict/
and released it to PyPI as http://pypi.python.org/pypi/bidict.



More information about the Python-list mailing list