标签:lis lan enc 差集 set 交集 list intersect union
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]
标签:lis lan enc 差集 set 交集 list intersect union
原文地址:https://www.cnblogs.com/douzi-m/p/13098593.html