Get difference between two lists

前端 未结 27 2836
傲寒
傲寒 2020-11-21 11:36

I have two lists in Python, like these:

temp1 = [\'One\', \'Two\', \'Three\', \'Four\']
temp2 = [\'One\', \'Two\']

I need to create a third

相关标签:
27条回答
  • 2020-11-21 11:51

    Let's say we have two lists

    list1 = [1, 3, 5, 7, 9]
    list2 = [1, 2, 3, 4, 5]
    

    we can see from the above two lists that items 1, 3, 5 exist in list2 and items 7, 9 do not. On the other hand, items 1, 3, 5 exist in list1 and items 2, 4 do not.

    What is the best solution to return a new list containing items 7, 9 and 2, 4?

    All answers above find the solution, now whats the most optimal?

    def difference(list1, list2):
        new_list = []
        for i in list1:
            if i not in list2:
                new_list.append(i)
    
        for j in list2:
            if j not in list1:
                new_list.append(j)
        return new_list
    

    versus

    def sym_diff(list1, list2):
        return list(set(list1).symmetric_difference(set(list2)))
    

    Using timeit we can see the results

    t1 = timeit.Timer("difference(list1, list2)", "from __main__ import difference, 
    list1, list2")
    t2 = timeit.Timer("sym_diff(list1, list2)", "from __main__ import sym_diff, 
    list1, list2")
    
    print('Using two for loops', t1.timeit(number=100000), 'Milliseconds')
    print('Using two for loops', t2.timeit(number=100000), 'Milliseconds')
    

    returns

    [7, 9, 2, 4]
    Using two for loops 0.11572412995155901 Milliseconds
    Using symmetric_difference 0.11285737506113946 Milliseconds
    
    Process finished with exit code 0
    
    0 讨论(0)
  • 2020-11-21 11:51

    I prefer to use converting to sets and then using the "difference()" function. The full code is :

    temp1 = ['One', 'Two', 'Three', 'Four'  ]                   
    temp2 = ['One', 'Two']
    set1 = set(temp1)
    set2 = set(temp2)
    set3 = set1.difference(set2)
    temp3 = list(set3)
    print(temp3)
    
    

    Output:

    >>>print(temp3)
    ['Three', 'Four']
    

    It's the easiest to undersand, and morover in future if you work with large data, converting it to sets will remove duplicates if duplicates are not required. Hope it helps ;-)

    0 讨论(0)
  • 2020-11-21 11:52

    Can be done using python XOR operator.

    • This will remove the duplicates in each list
    • This will show difference of temp1 from temp2 and temp2 from temp1.

    set(temp1) ^ set(temp2)
    
    0 讨论(0)
  • 2020-11-21 11:52
    def diffList(list1, list2):     # returns the difference between two lists.
        if len(list1) > len(list2):
            return (list(set(list1) - set(list2)))
        else:
            return (list(set(list2) - set(list1)))
    

    e.g. if list1 = [10, 15, 20, 25, 30, 35, 40] and list2 = [25, 40, 35] then the returned list will be output = [10, 20, 30, 15]

    0 讨论(0)
  • 2020-11-21 11:53

    most simple way,

    use set().difference(set())

    list_a = [1,2,3]
    list_b = [2,3]
    print set(list_a).difference(set(list_b))
    

    answer is set([1])

    can print as a list,

    print list(set(list_a).difference(set(list_b)))
    
    0 讨论(0)
  • 2020-11-21 11:53

    if you want something more like a changeset... could use Counter

    from collections import Counter
    
    def diff(a, b):
      """ more verbose than needs to be, for clarity """
      ca, cb = Counter(a), Counter(b)
      to_add = cb - ca
      to_remove = ca - cb
      changes = Counter(to_add)
      changes.subtract(to_remove)
      return changes
    
    lista = ['one', 'three', 'four', 'four', 'one']
    listb = ['one', 'two', 'three']
    
    In [127]: diff(lista, listb)
    Out[127]: Counter({'two': 1, 'one': -1, 'four': -2})
    # in order to go from lista to list b, you need to add a "two", remove a "one", and remove two "four"s
    
    In [128]: diff(listb, lista)
    Out[128]: Counter({'four': 2, 'one': 1, 'two': -1})
    # in order to go from listb to lista, you must add two "four"s, add a "one", and remove a "two"
    
    0 讨论(0)
提交回复
热议问题