python求两个列表的并集.交集.差集

求两个列表的差集

>>> a = [1,2,3]
>>> b=[1,2]
>>> ####################################
>>> #两个列表的差集
>>> ret = []
>>> for i in a:
    if i not in b:
         ret.append(i)
 
>>> ret
[3]
>>> #两个列表的差集2
>>> ret2 = [ i for i in a if i not in b ]
>>> ret2
[3]
>>> #两个列表的差集3
>>> ret3 = list(set(a) ^ set(b))
>>> ret3
[3]
>>> #两个列表的差集4
>>> ret4=list(set(a).difference(set(b))) #  b中有而a中没有的
>>> ret4
[3]

求两个列表的并集

>>> ########################################
>>> #获取两个list 的并集
>>> ret1=list(set(a).union(set(b)))
>>> ret1
[1, 2, 3]
>>> ret1=list(set(a).union(set([4,5,6])))
>>> ret1
[1, 2, 3, 4, 5, 6]
>>> #获取两个list 的并集2
>>> ret2= list(set(a) | set(b))
>>> ret2
[1, 2, 3]
>>> ret2=list(set(a)-set(b))#####差集
>>> ret2
[3]

求两个列表的交集

>>> ##########################################
>>> #获取两个列表的交集
>>> a = [1, 2, 3, 4, 5, 6]
>>> b = [2, 4, 6, 8 ,10]
>>> ret1= [x for x in b if x in set(a)]   # list a is the larger list b
>>> ret1
[2, 4, 6]
>>> #获取两个列表的交集2
>>> ret2= list(set(a) & set(b))
>>> ret2
[2, 4, 6]
>>> #获取两个列表的交集3
>>> ret3= list(set(a).intersection(b))
>>> ret3
[2, 4, 6]
>>>  #获取两个列表的交集4
>>> ret4 = list((set(a).union(set(b)))^(set(a)^set(b)))
>>> ret4
[2, 4, 6]
>>>

转自:https://blog.csdn.net/manjhok/article/details/79584110 

posted @ 2019-02-19 21:22  小白°  阅读(29187)  评论(0编辑  收藏  举报