Python intersection and difference sum doesn't give me the actual number of the original set

亡梦爱人 提交于 2019-11-29 16:34:22

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.

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)"

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!