时间:2021-05-18 09:48:42 | 栏目:Python代码 | 点击:次
1. 获取两个list 的交集
# -*- coding=utf-8 -*- #方法一: a=[2,3,4,5] b=[2,5,8] tmp = [val for val in a if val in b] print tmp #[2, 5] #方法二 print list(set(a).intersection(set(b)))
2. 获取两个list 的并集
print list(set(a).union(set(b)))
3. 获取两个list 的差集
print list(set(b).difference(set(a))) # b中有而a中没有的 print list(set(a).difference(set(b))) # a中有而b中没有的
总体代码及执行结果:
# -*- coding=utf-8 -*- #方法一: a=[2,3,4,5] b=[2,5,8] tmp = [val for val in a if val in b] print tmp #[2, 5] #方法二 print list(set(a).intersection(set(b))) print list(set(a).union(set(b))) print list(set(b).difference(set(a))) # b中有而a中没有的 print list(set(a).difference(set(b))) # a中有而b中没有的
/usr/bin/python /Users/nisj/PycharmProjects/EsDataProc/mysql_much_tab_data_static.py
[2, 5]
[2, 5]
[2, 3, 4, 5, 8]
[8]
[3, 4]
Process finished with exit code 0