用python编写一个程序:给定2个整数集合M和N,1.输入集合M和N的并集、交集和差集。2.升序输出他们的对等差分。3.输入格式:第一行输入用空格分隔的M个整数,第二行输入用空格分隔的N个整数
时间: 2024-05-06 17:16:50 浏览: 59
python获得两个数组交集、并集、差集的方法
5星 · 资源好评率100%
以下是Python程序的实现,包括输入两个整数集合M和N,计算并集、交集和差集,并升序输出对等差分:
```python
# 输入集合M和N
M = set(map(int, input().split()))
N = set(map(int, input().split()))
# 计算并集、交集和差集
union_set = M.union(N)
intersection_set = M.intersection(N)
diff_set = M.difference(N)
# 输出并集、交集和差集
print("Union set: ", union_set)
print("Intersection set: ", intersection_set)
print("Difference set: ", diff_set)
# 计算对等差分
sym_diff = sorted(union_set.symmetric_difference(intersection_set))
# 输出对等差分
print("Symmetric difference: ", sym_diff)
```
示例输入:
```
1 2 3 4 5
4 5 6 7 8
```
示例输出:
```
Union set: {1, 2, 3, 4, 5, 6, 7, 8}
Intersection set: {4, 5}
Difference set: {1, 2, 3}
Symmetric difference: [1, 2, 3, 6, 7, 8]
```
阅读全文