Difference Between Two Lists with Duplicates in Python

后端 未结 5 1123
灰色年华
灰色年华 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:26

    To take into account both duplicates and the order of elements:

    from collections import Counter
    
    def list_difference(a, b):
        count = Counter(a) # count items in a
        count.subtract(b)  # subtract items that are in b
        diff = []
        for x in a:
            if count[x] > 0:
               count[x] -= 1
               diff.append(x)
        return diff
    

    Example

    print(list_difference("z y z x v x y x u".split(), "x y z w z".split()))
    # -> ['y', 'x', 'v', 'x', 'u']
    

    Python 2.5 version:

    from collections import defaultdict 
    
    def list_difference25(a, b):
        # count items in a
        count = defaultdict(int) # item -> number of occurrences
        for x in a:
            count[x] += 1
    
        # subtract items that are in b
        for x in b: 
            count[x] -= 1
    
        diff = []
        for x in a:
            if count[x] > 0:
               count[x] -= 1
               diff.append(x)
        return diff
    
    0 讨论(0)
  • 2020-11-30 06:32

    You didn't specify if the order matters. If it does not, you can do this in >= Python 2.7:

    l1 = ['a', 'b', 'c', 'b', 'c']
    l2 = ['a', 'b', 'c', 'b']
    
    from collections import Counter
    
    c1 = Counter(l1)
    c2 = Counter(l2)
    
    diff = c1-c2
    print list(diff.elements())
    
    0 讨论(0)
  • 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))
    
    0 讨论(0)
  • 2020-11-30 06:34

    you can try this

    list(filter(lambda x:l1.remove(x),li2)) print(l1)

    0 讨论(0)
  • 2020-11-30 06:39

    Counters are new in Python 2.7. For a general solution to substract a from b:

    def list_difference(b, a):
        c = list(b)
        for item in a:
           try:
               c.remove(item)
           except ValueError:
               pass            #or maybe you want to keep a values here
        return c
    
    0 讨论(0)
提交回复
热议问题