说明
工作中遇到的小问题记录一下(还是太菜了),主要的是取两个list相同元素
使用
取交集,也就是两个list相同内容
a = [1,5,6,7,9]
b = [1,7,4,2,3]
print(list(set(a).intersection(set(b))))
`输出 1,7
`
取并集,去除重复的合并成一个list
a = [1,5,6,7,9]
b = [1,7,4,2,3]
print(list(set(a).union(set(b))))
`输出[1, 2, 3, 4, 5, 6, 7, 9]
`
获取两个 list 的差集
a = [1,5,6,7,9]
b = [1,7,4,2,3]
print(list(set(b).difference(set(a)))) # b中有而a中没有的
`输出[2, 3, 4]
`