一.两个list差集

a = [1,2,3]

b = [2,3]

想要的结果是[1]

下面记录一下三种实现方式:

1. 正常的方式

ret = []
for i in a:
if i not in b:
ret.append(i)

2.简化版

ret = [ i for i in a if i not in b ]

3.高级版

ret = list(set(a) ^ set(b))

4.最终版

ret =(list(set(b).difference(set(a)))) # b中有而a中没有的

 

二.两个list并集

print (list(set(a).union(set(b))))

三.两个list交集

print (list(set(a).intersection(set(b))))

 

原文链接:https://blog.csdn.net/liao392781/article/details/80577483

相关文章:

  • 2021-06-22
  • 2022-12-23
  • 2021-09-17
  • 2021-12-08
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-04
  • 2022-01-10
  • 2021-07-01
相关资源
相似解决方案