[Edu-sig] Reversing dictionaries, closures, permutations etc.
Terry Hancock
hancock at anansispaceworks.com
Fri Jan 23 17:04:47 EST 2004
On Friday 23 January 2004 01:56 pm, Kirby Urner wrote:
> How to reverse a dictionary? The dict class has no .reverse() method, which
> makes sense, as keys must be immutable so there's no guarantee the reverse
> is even valid (throwing an exception would be the way to go in this case).
>a straightforward way to do it in code would be:
Be aware also that the reverse (or inverse) may also be degenerate, unless
you have a strict 1:1 correspondence between keys and values. There are
cases of use of course, and bear in mind that the inverse of the inverse of
dictionary is *guaranteed* to be 1:1. I use something like this when I need
to have such a guarantee and want a reversible mapping.
Cheers,
Terry
--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com
More information about the Edu-sig
mailing list