随机生成10个0~100之间的整数,组成集合A,同样生成10个随机整数组成集合B。输出A和B的内容、长度、最大值、最小值以及它们的并集、交集和差集。
时间: 2024-10-09 12:04:26 浏览: 48
首先,我们需要导入Python的random模块来生成随机整数。接下来,我们可以按照你的需求操作两个集合。
```python
import random
# 生成集合A
A = set(random.sample(range(0, 101), 10))
# 生成集合B
B = set(random.sample(range(0, 101), 10))
# 输出集合A和B的内容
print("集合A:", A)
print("集合B:", B)
# 计算长度
length_A = len(A)
length_B = len(B)
print("集合A长度:", length_A)
print("集合B长度:", length_B)
# 获取最大值和最小值
max_value_A = max(A)
min_value_A = min(A)
max_value_B = max(B)
min_value_B = min(B)
print("集合A最大值:", max_value_A)
print("集合A最小值:", min_value_A)
print("集合B最大值:", max_value_B)
print("集合B最小值:", min_value_B)
# 并集、交集和差集
union_set = A.union(B)
intersection_set = A.intersection(B)
difference_set = A.difference(B)
print("并集(A∪B):", union_set)
print("交集(A∩B):", intersection_set)
print("差集(A-B):", difference_set)
```
注意:集合操作不会改变原集合,它们都是基于原集合的副本进行计算。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)