求两个列表的交集,并集,差集
l1 = [1, 2, 3, 4, 5, 6]
l2 = [3, 4, 5, 6, 7, 8, 9]
# 两个列表的交集
lst_intersection = [i for i in l1 if i in l2]
# print(lst_intersection) ----------->[3, 4, 5, 6]
# 两个列表的并集
lst_union = list(set(l1).union(set(l2)))
# print(lst_union) ---------->[1, 2, 3, 4, 5, 6, 7, 8, 9]
# 两个列表的差集
l1_diff = list(set(l1).difference(set(l2)))
# print('l1相对于l2的差集:',l1_diff) ----------> l1相对于l2的差集: [1, 2]
l2_diff = list(set(l2).difference(set(l1)))
# print('l2相对于l1的差集:',l2_diff) ----------> l2相对于l1的差集: [8, 9, 7]