New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
symmetric difference operation applicable to more than two sets #62054
Comments
The description of the symmetric difference operation implies that it cannot be applied to more than two sets (http://docs.python.org/3/library/stdtypes.html#set.symmetric_difference). However, this is certainly possible: >>> s={1,2}
>>> t={2,3}
>>> u={3,4}
>>> s^t^u
{1, 4}
>>> s.symmetric_difference(t).symmetric_difference(u)
{1, 4} I am not sure how much of a "semantic" sense that makes, given that symmetric difference is by definition for two sets. (http://en.wikipedia.org/wiki/Symmetric_difference). So, either the operator should be fixed to allow only two sets or the description be updated. |
On some more thought, perhaps the description should be updated. Since s^t^u is effectively (s^t)^u and hence the implementation does not violate the definition. |
Can you suggest a change? I don't see a problem here; giving multiple operators for the other operations does not imply that they are not treated as left-associative. |
I think the only change I am suggesting is the description of the ^ operator to be something like this: set ^ other ^ .. I do understand that this is not really what the operator and the corresponding operation symmetric_difference() allows semantically. But it does make it consistent with the description of operators such as the | or &, but then their operation allows multiple sets semantically. Hmm may be it is fine as it is.. |
Sets have methods that do not have operators (such as len, isdisjoint), A coherent proposal would change the method code and doc to the following: symmetric_difference(other, ...)
set ^ other ^ ...
Return a new set with elements in an odd number of the sets.
s={1,2, 5}
t={2,3, 5}
u={3,4, 5}
print(s^t^u)
>>>
{1, 4, 5} I believe the proposal was once considered, and rejected. An argument for is that the effect of chained symmetric differences is not obvious, as evidenced by Amit's mistaken characterization. I had to think a bit before I was sure of the answer. An argument against is that what one actually gets is seldom wanted, so that allowing more than two inputs to the method would have little benefit. What might be done is to document the symmetric different of multiple sets with a parenthetical comment such as "(The symmetric difference of multiple sets, a ^ b ^ c ^ ..., is a new set with elements appearing in an odd number of input sets.)" This would let people know what to expect from such expressions, in a situation where the effect is less obvious than usual. |
This wording is not really clear to me. IMHO the documentation is fine as is. The evaluation order works as usual, and, since the symmetric difference is an associative (and commutative) operation, the order doesn't even matter. |
If you take the union/intersection/symmetric difference of n sets, the result is a set with all items that appears in one/all/an odd number of the n sets. The union and intersection methods actually accept n inputs, because the result is obvious, useful, and can be obtained faster that with n-1 binary operations. The symmetric_difference method does not, I presume because the result in not obvious (but that cuts both ways), not known to be useful, and perhaps would not be much faster than than n-1 binary operations. |
I agree that the doc is fine as it is. If there will be no objections/suggestions in the next couple of weeks I will close this issue. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: