[Python-ideas] Dict with inverse form

Chris Barker chris.barker at noaa.gov
Fri Feb 12 15:36:38 EST 2016


On Fri, Feb 12, 2016 at 12:27 PM, Andrew Barnert via Python-ideas <
python-ideas at python.org> wrote:

> I look forward to it. Next time I need some variation of this, even if it
> *isn't* the same variation you end up building, the fact that there's a
> potential de facto standard for what to call the ".inv" or whatever still
> helps me, right?
>

yeah, though I'm not sure I like that name... (can't think of a better one
right now, though).

But what I would like is for the "inverse" to be available as an object
itself, so:

my_double_dict = DoubleDict( ( (1:'a'), (2:'b'), (3:'c) ) )
my_inverse = my_double_dict.inv

my_double_dict[1] == 'a'
my_inverse['a'] == 1


i.e you now have two objects, which are essentially the same object, but
with inverse referencing semantics.

Note how I cleverly introduced a name for this object -- I think it more as
doubling than inversing, or really a one-to-one mapping, but haven't
thought of a clever name for from that...

And for my part, re-using a key on either side should simply write-over the
existing item, just like a dict.

-CHB









> Also, even if nobody ever agrees to put this in the stdlib, the
> collections module linked to some outside recipes/modules like OrderedSet
> long before Nick and the other packaging guys started pushing the idea that
> it was a good idea for the stdlib in general. I think if this gets
> sufficient uptake, it deserves such a link. As you say, it's something many
> people end up building for themselves.
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160212/e14efc84/attachment.html>


More information about the Python-ideas mailing list