懒羊羊softwaretesting

导航

求两个列表的交集、并集、差集

a=[1,2,3,4]
b=[3,4,5,6]

jiaoji=[i for i in a if i in b]
print("a和b的交集:%s" % jiaoji)

bingji=list(set(a).union(set(b)))
print("a和b的并集: %s" % bingji)

chaji=set(a).difference(set(b))
print("a和b的差集:%s" %chaji)
chaji2=set(b).difference(set(a))
print("b和a的差集:%s" % chaji2)

posted on 2019-04-29 21:33  懒羊羊呐  阅读(4727)  评论(0编辑  收藏  举报