I am working on differences of lists.
>>a = [1, 2, 3]
>>b = [2, 4, 5]
>>c = [3, 2, 6]
Symmetric difference between 2 sets can be done using:
>>z = set(a).symmetric_difference(set(b))
>>print z
>>set([1, 3, 4, 5])
How to get difference between 3 sets? For difference of 3 sets, expected output is :
expected output : set([1, 3, 4, 5, 6])
Just subtract the intersection from the union:
Or, to generalise to an arbitrary collection of sets:
or:
(The latter is probably preferable.)
What about this:
If you want to exclude elements that are present more than once instead:
This methods accept any number of lists
How about this:
@NPE that answer doesn't work as expected since it relies on the result of the proceeding operations. In your example you can see the '3' overlapping from the first to the third but still being included in the result.
This happens because it does the intersection of 'a' to 'b' and then the intersection to 'c'. To fix this you could:
Also to save two operations you could get the the symmetric difference of two, union with the third, then do the difference of the union of the intersections of the first and third and second and third.