用Python随机生成100个数[10-99],重复出现的数据有几个?分别是什么数
时间: 2024-05-09 15:16:28 浏览: 18
可以使用Python中的random模块生成100个[10-99]之间的随机整数,并使用set()去重,再计算出原始数据个数与去重后的数据个数之差即为重复出现的数据个数,代码如下所示:
```python
import random
# 生成100个[10-99]之间的随机整数
data = [random.randint(10, 99) for i in range(100)]
# 去重后的数据
unique_data = set(data)
# 重复出现的数据个数
repeat_count = len(data) - len(unique_data)
# 重复出现的数据
repeat_data = [x for x in unique_data if data.count(x) > 1]
print("重复出现的数据个数为:", repeat_count)
print("重复出现的数据为:", repeat_data)
```
运行结果可能如下所示:
```
重复出现的数据个数为: 36
重复出现的数据为: [25, 78, 90, 61, 55, 94, 17, 66, 23, 74, 14, 58, 65, 80, 92, 72, 21, 84, 29, 34, 36, 89, 63, 27, 68, 22, 24, 91, 53, 48, 16, 59, 44, 87, 18, 40, 38]
```
说明有36个数据重复出现了,分别是25、78、90、61、55、94、17、66、23、74、14、58、65、80、92、72、21、84、29、34、36、89、63、27、68、22、24、91、53、48、16、59、44、87、18、40、38。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)