Get difference between two lists

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

    We can calculate intersection minus union of lists:

    temp1 = ['One', 'Two', 'Three', 'Four']
    temp2 = ['One', 'Two', 'Five']
    
    set(temp1+temp2)-(set(temp1)&set(temp2))
    
    Out: set(['Four', 'Five', 'Three']) 
    

提交回复
热议问题