Comparing two lists and only printing the differences? (XORing two lists)

前端 未结 6 1750
盖世英雄少女心
盖世英雄少女心 2020-12-09 11:55

I\'m trying to create a function that takes in 2 lists and returns the list that only has the differences of the two lists.

Example:

a = [1,2,5,7,9]         


        
相关标签:
6条回答
  • 2020-12-09 12:38

    You basically want to add an element to your new list if it is present in one and not present in another. Here is a compact loop which can do it. For each element in the two lists (concatenate them with list1+list2), we add element if it is not present in one of them:

    [a for a in list1+list2 if (a not in list1) or (a not in list2)]
    

    You can easily transform it into a more unPythonic code with explicit looping through elements as you have now, but honestly I don't see a point (not that it matters):

    def xor(list1, list2):
        outputlist = []
        list3 = list1 + list2
        for i in range(0, len(list3)):
            if ((list3[i] not in list1) or (list3[i] not in list2)) and (list3[i] not in outputlist):
                 outputlist[len(outputlist):] = [list3[i]]
        return outputlist
    
    0 讨论(0)
  • 2020-12-09 12:48

    Simple, but not particularly efficient :)

    >>> a = [1,2,5,7,9]
    >>> b = [1,2,4,8,9]
    >>> [i for i in a+b if (a+b).count(i)==1]
    [5, 7, 4, 8]
    

    Or with "just loops"

    >>> res = []
    >>> for i in a+b:
    ...  c = 0
    ...  for j in a+b:
    ...   if i==j:
    ...    c += 1
    ...  if c == 1:
    ...   res.append(i)
    ... 
    >>> res
    [5, 7, 4, 8]
    
    0 讨论(0)
  • 2020-12-09 12:53

    Note: This is really unpythonic and should only be used as a homework answer :)

    After you have sorted both lists, you can find duplicates by doing the following:

    1) Place iterators at the start of A and B

    2) If Aitr is greater than Bitr, advance Bitr after placing Bitr's value in the return list

    3) Else if Bitr is greater than Aitr, advance Aiter after placing Aitr's value in the return list

    4) Else you have found a duplicate, advance Aitr and Bitr

    0 讨论(0)
  • 2020-12-09 12:54

    Try this,

        a = [1,2,5,7,9]
        b = [1,2,4,8,9]
     print set(a).symmetric_difference(set(b))
    
    0 讨论(0)
  • 2020-12-09 12:59

    Use set is better

    >>> a = [1,2,5,7,9]
    >>> b = [1,2,4,8,9]
    >>> set(a).symmetric_difference(b)
    {4, 5, 7, 8}
    

    Thanks to @DSM, a better sentence is:

    >>> set(a)^set(b)
    

    These two statements are the same. But the latter is clearer.

    Update: sorry, I did not see the last requirement: cannot use set. As far as I see, the solution provided by @sashkello is the best.

    0 讨论(0)
  • 2020-12-09 12:59

    This code works assuming you've got sorted lists. It works in linear time, rather than quadratic like many of the other solutions given.

    def diff(sl0, sl1):
        i0, i1 = 0, 0
        while i0 < len(sl0) and i1 < len(sl1):
            if sl0[i0] == sl1[i1]:
                i0 += 1
                i1 += 1
            elif sl0[i0] < sl1[i1]:
                yield sl0[i0]
                i0 += 1
            else:
                yield sl1[i1]
                i1 += 1
        for i in xrange(i0, len(sl0)):
            yield sl0[i]
        for i in xrange(i1, len(sl1)):
            yield sl1[i]
    
    print list(diff([1,2,5,7,9], [1,2,4,8,9]))
    
    0 讨论(0)
提交回复
热议问题