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

前端 未结 6 1769
盖世英雄少女心
盖世英雄少女心 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: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.

提交回复
热议问题