从键盘输入两个非空集合求集合的交集并集和差集
时间: 2023-12-24 15:52:40 AIGC 浏览: 240
好的,让我来写一个简单的 Python 代码来实现这个功能:
```python
set1 = set(input("请输入第一个集合,元素之间用空格隔开:").split())
set2 = set(input("请输入第二个集合,元素之间用空格隔开:").split())
intersection = set1.intersection(set2)
union = set1.union(set2)
difference1 = set1.difference(set2)
difference2 = set2.difference(set1)
print("集合的交集为:", intersection)
print("集合的并集为:", union)
print("第一个集合减去第二个集合的差集为:", difference1)
print("第二个集合减去第一个集合的差集为:", difference2)
```
代码运行后,程序会要求输入两个非空集合,然后输出它们的交集、并集以及两个集合之间的差集。注意,这里使用了 Python 的 `set` 类型来保存集合,因为 `set` 类型支持集合的交、并、差等操作。
阅读全文
相关推荐







