python列表取差集
时间: 2023-11-24 14:51:08 浏览: 115
基于python求两个列表的并集.交集.差集
以下是两种Python列表取差集的方法:
1.使用set()函数
```python
a = [1, 2, 3, 4, 5]
b = [3, 4, 5, 6, 7]
diff = list(set(a).difference(set(b)))
print(diff) # 输出:[1, 2]
```
2.使用列表推导式
```python
a = [1, 2, 3, 4, 5]
b = [3, 4, 5, 6, 7]
diff = [i for i in a if i not in b]
print(diff) # 输出:[1, 2]
```
阅读全文