Magdoll wrote:
> Is there a cleaner way to do this example:
>
> d = {('a','b'): 10, ('a','c'): 20, ('b','c'): 30}
>
> The key is always a pair (x,y), but d[(x,y)] should have the same
> result as d[(y,x)]. So either I would have to store both d[(x,y)] and
> d[(y,x)] (unncessary extra space?), or write something like:
>
> if x <= y: return d[(x,y)]
> else:       return d[(y,x)]
>
> I'm not familiar with python enough, so I want to know whether these
> are my only choices....
>   
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.

Gary Herron

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to