On Sat, Jul 6, 2013 at 7:23 PM, Steven D'Aprano <steve@pearwood.info> wrote:
For the record, your suggestion was to add a convenience method:
set1.partition(set2) => (set1 - set2, set1 & set2)
only1, both = set1.partition(set2)
only2 = set2 - set1

instead of Paddy's suggestion (with the method name left unknown):

only1, both, only2 = set1.???????(set2)

I dislike the appearance of creating a method on the 'set' type.  It creates an asymmetry between the respective sets that doesn't really express the sense of the symmetrical operation.

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.

Therefore, I would suggest adding a C-coded function in the STDLIB--probably in 'collections' to do this.  E.g.

  from collections import segment  # tentative name, see various suggestions in thread
  only1, only2, intersection = segment(iter1, iter2)

In behavior, this should do the same thing as the below (just faster):

  def segment(iter1, iter2):
      set1, set2 = map(set, (iter1, iter2))
      return set1 - set2, set2 - set1, set1 & set2


 
--
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.