I would definitely use such a construction. It would be nice to have a data structure that behaves like 1:1 correspondence or as in your case 1 to many correspondances instead of needing to keep two dictionaries. It would make life a lot easier because you loose the need of a double administration. I.e. if you now want to remove 'c' from d in your example you would need to do:
v = d.pop('c') d_inv[v].remove('c') it would by much nicer (and less error prone) if one could just do: del d['c'] Le mercredi 25 septembre 2013 18:08:12 UTC+2, Nathann Cohen a écrit : > > Helloooooooo everybody ! > > I was just messing with dictionaries and lists, and I wonder if we > could solve the problem once and for all with an inefficient generic > solution. Here's the thing : > > I often have to define both a function, and its inverse. Something like : > > d = { > 'a' : 1, > 'b' : 2, > 'c' : 1, > 'd' : 3 > } > > Then, I want to find the list of all elements whose image is a 1, or > 2, or 3, and end up defining the following dictionary : > > d_inv = { > 1 : ['a','c'], > 2 : ['b'], > 3 : ['d'] > } > > Aaaaaand it would be sooo nice if there was a way to write d**(-1)[2], > or something of the kind ! Did you ever write a code like this, and > would you be interested by a generic tool for that ? > > Otherwise I'll just keep on computing the inverse of my dictionaries > with a couple of Python lines ;-) > > Have fuuuuuuuun ! > > Nathann > -- You received this message because you are subscribed to the Google Groups "sage-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+unsubscr...@googlegroups.com. To post to this group, send email to sage-devel@googlegroups.com. Visit this group at http://groups.google.com/group/sage-devel. For more options, visit https://groups.google.com/groups/opt_out.