# 差集(difference) 交集(intersection) 并集(union)

list_aa = ['xiaohua', 'haha', 'dada', 'aa']

list_bb = ['zhuangzhou', 'haha', 'dada', 'bb']

# 差集
# list_aa与list_bb的差集
difference_aa = set(list_aa).difference(set(list_bb))
print(difference_aa)
# {'xiaohua', 'aa'}


# list_bb与list_aa的差集
difference_bb = set(list_bb).difference(set(list_aa))
print(difference_bb)
# {'zhuangzhou', 'bb'}


# 交集
intersection_aa_bb = set(list_aa).intersection(set(list_bb))
print(intersection_aa_bb)
# {'dada', 'haha'}


# 并集
union_aa_bb = set(list_aa).union(list_bb)
print(union_aa_bb)
# {'zhuangzhou', 'xiaohua', 'aa', 'bb', 'haha', 'dada'}

 

posted on 2022-04-29 13:37  山海经-小怪  阅读(172)  评论(0编辑  收藏  举报