求a_set = set([8, 9, 10, 11, 12, 13])和b_set = {0, 1, 2, 3, 7, 8}的交集,并集,差集,对称差集的python代码
时间: 2023-10-07 15:05:26 浏览: 74
代码如下:
```
a_set = set([8, 9, 10, 11, 12, 13])
b_set = {0, 1, 2, 3, 7, 8}
# 交集
intersection_set = a_set.intersection(b_set)
print("交集:", intersection_set)
# 并集
union_set = a_set.union(b_set)
print("并集:", union_set)
# 差集
difference_set = a_set.difference(b_set)
print("差集(a_set - b_set):", difference_set)
# 对称差集
symmetric_difference_set = a_set.symmetric_difference(b_set)
print("对称差集:", symmetric_difference_set)
```
输出结果如下:
```
交集: {8}
并集: {0, 1, 2, 3, 7, 8, 9, 10, 11, 12, 13}
差集(a_set - b_set): {9, 10, 11, 12, 13}
对称差集: {0, 1, 2, 3, 7, 9, 10, 11, 12, 13}
```
阅读全文