分别有两个集合{1,2,1,3,4,5,6,7},{1,2,3,8,9,7,10},求两个集合的差集、并集、交集
时间: 2024-03-18 15:35:55 浏览: 173
以下是两个集合的差集、并集和交集的演示:
1. 差集:
```python
set1 = {1, 2, 1, 3, 4, 5, 6, 7}
set2 = {1, 2, 3, 8, 9, 7, 10}
difference = set1 - set2
print("差集:", difference) # 输出:{4, 5, 6}
```
2. 并集:
```python
set1 = {1, 2, 1, 3, 4, 5, 6, 7}
set2 = {1, 2, 3, 8, 9, 7, 10}
union = set1 | set2
print("并集:", union) # 输出:{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
```
3. 交集:
```python
set1 = {1, 2, 1, 3, 4, 5, 6, 7}
set2 = {1, 2, 3, 8, 9, 7, 10}
intersection = set1 & set2
print("交集:", intersection) # 输出:{1, 2, 3, 7}
```
阅读全文