Get difference between two lists

前端 未结 27 2834
傲寒
傲寒 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:45

    In [5]: list(set(temp1) - set(temp2))
    Out[5]: ['Four', 'Three']
    

    Beware that

    In [5]: set([1, 2]) - set([2, 3])
    Out[5]: set([1]) 
    

    where you might expect/want it to equal set([1, 3]). If you do want set([1, 3]) as your answer, you'll need to use set([1, 2]).symmetric_difference(set([2, 3])).

提交回复
热议问题