Skip to content
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

Closed
AmitSaha mannequin opened this issue Apr 27, 2013 · 8 comments
Closed

symmetric difference operation applicable to more than two sets #62054

AmitSaha mannequin opened this issue Apr 27, 2013 · 8 comments
Labels
docs Documentation in the Doc dir type-feature A feature request or enhancement

Comments

@AmitSaha
Copy link
Mannequin

AmitSaha mannequin commented Apr 27, 2013

BPO 17854
Nosy @birkenfeld, @rhettinger, @terryjreedy, @ezio-melotti, @iritkatriel

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:

assignee = None
closed_at = <Date 2020-11-18.00:45:04.341>
created_at = <Date 2013-04-27.11:47:20.201>
labels = ['type-feature', 'docs']
title = 'symmetric difference operation applicable to more than two sets'
updated_at = <Date 2020-11-18.00:45:04.341>
user = 'https://bugs.python.org/AmitSaha'

bugs.python.org fields:

activity = <Date 2020-11-18.00:45:04.341>
actor = 'rhettinger'
assignee = 'docs@python'
closed = True
closed_date = <Date 2020-11-18.00:45:04.341>
closer = 'rhettinger'
components = ['Documentation']
creation = <Date 2013-04-27.11:47:20.201>
creator = 'Amit.Saha'
dependencies = []
files = []
hgrepos = []
issue_num = 17854
keywords = []
message_count = 8.0
messages = ['187899', '187900', '187901', '187902', '188328', '188376', '188415', '381270']
nosy_count = 7.0
nosy_names = ['georg.brandl', 'rhettinger', 'terry.reedy', 'ezio.melotti', 'docs@python', 'Amit.Saha', 'iritkatriel']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue17854'
versions = ['Python 2.7', 'Python 3.3', 'Python 3.4']

@AmitSaha
Copy link
Mannequin Author

AmitSaha mannequin commented Apr 27, 2013

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.

@AmitSaha AmitSaha mannequin added the type-bug An unexpected behavior, bug, or error label Apr 27, 2013
@AmitSaha AmitSaha mannequin assigned docspython Apr 27, 2013
@AmitSaha AmitSaha mannequin added the docs Documentation in the Doc dir label Apr 27, 2013
@AmitSaha
Copy link
Mannequin Author

AmitSaha mannequin commented Apr 27, 2013

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.

@birkenfeld
Copy link
Member

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.

@AmitSaha
Copy link
Mannequin Author

AmitSaha mannequin commented Apr 27, 2013

I think the only change I am suggesting is the description of the ^ operator to be something like this:

set ^ other ^ ..
Return a new set with elements from the sets which are not present in more than one set

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..

@terryjreedy
Copy link
Member

Sets have methods that do not have operators (such as len, isdisjoint),
operators that do not have non-special methods (such as in, <), and method-operator pairs that do the same thing (such as (union, |), (symmetric_difference, ^)). For the pairs, it gives the method signature and the *equivalent* operator expression. Since .union takes multiple 'other' args, the equivalent operator expression does too. Since .symmetric_difference only takes one 'other' arg, so does the expression.

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.

@ezio-melotti
Copy link
Member

Return a new set with elements in an odd number of the sets.

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.

@ezio-melotti ezio-melotti added type-feature A feature request or enhancement and removed type-bug An unexpected behavior, bug, or error labels May 4, 2013
@terryjreedy
Copy link
Member

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.

@iritkatriel
Copy link
Member

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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

5 participants