Opposite of set.intersection in python?

前端 未结 5 553
你的背包
你的背包 2020-12-24 11:20

In Python you can use a.intersection(b) to find the items common to both sets.

Is there a way to do the disjoint opposite version of this? Ite

5条回答
  •  爱一瞬间的悲伤
    2020-12-24 12:18

    The best way is a list comprehension.

    a = [ 1,2,3,4]
    b = [ 8,7,9,2,1]
    c = [ element for element in a if element not in b] 
    d = [ element for element in b if element not in a] 
    print(c) 
    # output is [ 3,4]
    print(d) 
    # output is  [8,7,9]
    

    You can join both lists

提交回复
热议问题