python 集合运算交集&并集&差集

差集
>>> #两个列表的差集3 >>> ret3 = list(set(a) ^ set(b)) #两个列表的差集 >>> ret4=list(set(a).difference(set(b))) # a not same b ,retrun = a-b if a-b>0 else {} 并集 获取两个list 的并集 >>> ret1=list(set(a).union(set(b)))
>>> #获取两个list 的并集2 >>> ret2= list(set(a) | set(b)) 交集 >>> #获取两个列表的交集2 >>> ret2= list(set(a) & set(b)) >>> ret2 >>> #获取两个列表的交集3 >>> ret3= list(set(a).intersection(b))
>> #获取两个列表的交集4 >>> ret4 = list((set(a).union(set(b)))^(set(a)^set(b))) >>> ret4

  

原文地址:https://www.cnblogs.com/SunshineKimi/p/12357987.html