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

Steven D'Aprano steve at pearwood.info
Sat Jul 6 15:38:54 CEST 2013


On 05/07/13 19:48, Oscar Benjamin wrote:
> On 5 July 2013 08:09, Paddy3118 <paddy3118 at gmail.com> wrote:
>> On Friday, 5 July 2013 07:19:24 UTC+1, Greg Ewing wrote:
>>> Oscar Benjamin wrote:
>>>> On 4 July 2013 03:10, Steven D'Aprano <st... at pearwood.info> wrote:
>>>>>
>>>>> Does any other language with sets offer this as a set primitive?
>>>>
>>>> I'd be interested to know what they call it if they do.
>>>
>>> I think it should be called vennerate().

Groan. That's a terrible pun :-)


>> Vennerate?
>> Cute; but we have str1.partition(str2), and set1.partition(set2) would be
>> mathematical partition of the union of both sets returning a tuple of three
>> elements - just like str.partition, which makes me vote for the name
>> "partition".
>
> I didn't know about the str.partition method but having looked at it
> now what it does is closer to what I think of as a partition of a set
> i.e. after
>
>      head, sep, tail = str1.partition(str2)
>
> we have that
>
>      str1 == head + sep + tail
>
> By analogy I would expect that after

In this case, what's being partitioned isn't set1, but the union of the two sets.


>      set2, set3, set4 = set1.partition(...)
>
> we would have
>
>      set1 == set2 | set3 | set4
>
> because that is (one property of) a "partition" of set1. However this
> is not the meaning you intend.

set1 | set2 = only1 | common | only2


[only a quarter serious]
In a way, perhaps this ought to be an operator, and return a set of three (frozen)sets.



"partition" is not the ideal name for this method, but the actual operation itself is very useful. I have often done this, mostly on dict views rather than sets. In my head, it's an obvious operation, to split a pair of sets into three, spoiled only by lack of a good name.

Perhaps "split" is a reasonable name?

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


Hmmm... seems reasonable to me.


-- 
Steven


More information about the Python-ideas mailing list