[Python-ideas] exclusively1, common, exclusively2 = set1 - set2, set1 & set2, set2 - set1

Andrew Barnert abarnert at yahoo.com
Sun Jul 7 05:40:37 CEST 2013


On Jul 6, 2013, at 19:53, David Mertz <mertz at gnosis.cx> wrote:

> However, I *do* think it would be worth having a faster (i.e. C-coded) way of doing the three-way partitioning.  Moreover, there's really no reason that such a function couldn't operate on collections (or iterators) other than sets, and being general seems more useful.

If we don't have "intersection" and "difference" functions that work on any pair of iterables, it seems strange to have this function do so.

And I think there's a good reason we don't have them, and that same reason applies here: there actually _is_ an inherent asymmetry to all of these algorithms. One of the iterables has to be a set (or at least a Set with fast lookup); the other can be anything, with no change in semantics or performance. 

In fact, if the large iterable is an iterator, there could even be a substantial improvement in space if the result were three iterators. (Obviously O(M) is much better than O(N+M) when M << N.) However, going again by the parallel with the methods we already have, if we don't have itertools.set_difference we probably don't need itertools.whatever_this_is, just a method on set.


More information about the Python-ideas mailing list