different key, same value in dictionaries

Matt Nordhoff mnordhoff at mattnordhoff.com
Sun Feb 10 00:46:12 CET 2008


Gary Herron wrote:
> You could use ImmutableSets as indexes.  (In fact this is the whole
> reason for the existence of ImmutableSets.)
> 
> You could derive your own dictionary type from the builtin dictionary
> type, and map an index operation d[(x,y)] to
> d[ImmutableSet(a,b)].  Then all of d[a,b], d[b,a], d[(a,b)] and d[(b,a)]
> would index the same element.
> 
> See http://docs.python.org/lib/module-sets.html for details of the set
> module.

Since Python 2.4, sets are built-in types. Use "set" and "frozenset"
instead of "sets.Set" and "sets.ImmutableSet", respectively.

If you need Python 2.3 compatibility, you can do something like this:

try:
    set, frozenset
except NameError:
    from sets import Set as set, ImmutableSet as frozenset
-- 



More information about the Python-list mailing list