Python Intersection And Difference Sum Doesn't Give Me The Actual Number Of The Original Set
I have two lists, one with old IDs and one with new IDs. I want to get items in common and items not common. The new_Items list has all new ones. The old_Items has the old ones. I
Solution 1:
What you're looking for is called the "symmetric difference".
set(new_Items) ^ set(old_Items)
Or,
set(new_Items).symmetric_difference(old_Items)
This gives you items that belong to either set, but not both. You are currently computing only those items that belong to new_Items
, but not the other way round, hence the discrepancy.
Refer to the set.symmetric_difference
docs.
Solution 2:
A-B gives items of A which are not in B B-A gives items of B which are not in A Either of these is not what you are looking for
For items not common you need (A union B) minus (A intersection B) which is the symmetric difference of sets
You can also get by "(A-B) union (B-A)"
Solution 3:
The list had some repeated items, that was the problem.
So the set cuts these repeated items and that is why printing less numbers than I expect.
Post a Comment for "Python Intersection And Difference Sum Doesn't Give Me The Actual Number Of The Original Set"