Set operations on object attributes question

Raymond Hettinger python at rcn.com
Tue Oct 23 16:33:41 EDT 2007


> Since what I _really_ wanted from this was the intersection of the
> objects (based on attribute 2), I was looking for a set-based
> solution,
> kinda like a decorate - <set operation> - undecorate pattern. Perhaps
> the problem does not fall into that category.

The "kinda" part is where the idea falls down.  If you've decorated
the inputs with a key function (like the key= argument to sorted()),
then the intersection step will return only a single element result,
not both matches as you specified in your original request.  IOW,
you cannot get set([(2, 1, 3), (1, 3, 3)]) as a result if both
set members are to be treated as equal.

It would be rather like writing set([1]).intersection(set([1.0]))
and expecting to get set([1, 1.0]) as a result.  Does your
intuition support having an intersection return a set larger
than either of the two inputs?


Raymond




More information about the Python-list mailing list