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

前端 未结 3 1371
攒了一身酷
攒了一身酷 2020-12-22 00:30

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_It

相关标签:
3条回答
  • 2020-12-22 00:59

    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.

    0 讨论(0)
  • 2020-12-22 01:08

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

    0 讨论(0)
  • 2020-12-22 01:20

    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.

    0 讨论(0)
提交回复
热议问题