Difference Between Two Lists with Duplicates in Python

后端 未结 5 1226
灰色年华
灰色年华 2020-11-30 06:09

I have two lists that contain many of the same items, including duplicate items. I want to check which items in the first list are not in the second list. For example, I mig

5条回答
  •  青春惊慌失措
    2020-11-30 06:33

    Create Counters for both lists, then subtract one from the other.

    from collections import Counter
    
    a = [1,2,3,1,2]
    b = [1,2,3,1]
    
    c = Counter(a)
    c.subtract(Counter(b))
    

提交回复
热议问题