Python求交集,并集,差集,补集

#交集
a = [1,2,3,4] b = [2,4,5,5,7] intersection = list(set(a) & set(b))

>>[2,4]

#并集
a = [1,2,3] b = [5,7,3] unionset = list(set(a).union(set(b)))

>>[1,2,3,5,7]

#差集
a = [1,2,3] b = [1,3,5] #b有而a没有的元素 difference = list(set(b).difference(set(a)))

>>[5]

#补集
a = [2,3]
b = [1,2,5,7]
complementaryset = list(set(b) - (set(a)))

>>[1,5,7]

posted @ 2020-09-16 21:22  XXXSANS  阅读(1487)  评论(0编辑  收藏  举报