使用列表生成式随机产生10个两位的正整数,存入列表ls中,输出ls中的这10个随机数,然后对这10个随机数求平均值,并输出统计高于平均值的数有多少个。
时间: 2024-01-21 19:18:59 浏览: 32
可以使用random模块中的randint函数来生成随机数,然后使用列表生成式来生成10个随机数,再计算平均值和高于平均值的数的个数。
代码如下:
```python
import random
# 生成10个随机数,存入列表ls中
ls = [random.randint(10, 99) for i in range(10)]
# 输出ls中的随机数
print("随机数列表:", ls)
# 计算平均值
average = sum(ls) / len(ls)
# 统计高于平均值的数的个数
count = sum(1 for x in ls if x > average)
# 输出结果
print("平均值:", average)
print("高于平均值的数的个数:", count)
```
运行结果示例:
```
随机数列表: [30, 27, 56, 13, 25, 74, 58, 60, 58, 47]
平均值: 43.8
高于平均值的数的个数: 4
```
相关问题
使用列表生成式随机产生10个两位 的正整数,存入列表ls中
好的,我可以帮你写这个列表生成式。
```python
import random
ls = [random.randint(10, 99) for i in range(10)]
print(ls)
```
这个列表生成式使用了 random 模块来生成随机数,其中 randint(10, 99) 用于生成两位正整数。方括号内的 for i in range(10) 表示需要生成 10 个随机数,最终结果保存在 ls 列表中。运行程序可以得到输出:
```
[34, 92, 69, 16, 75, 56, 38, 50, 15, 11]
```
使用列表生成式随机产生10个两位的正整数,存入列表ls中,输出ls中的这10个随机数,然后对这10个随机数求平均值,并输出统计小于平均值的数有多少个。
以下是使用Python编写的程序,用于随机产生10个两位的正整数,存入列表ls中,并输出这10个随机数,求平均值,并输出小于平均值的数的个数:
```
import random
# 使用列表生成式随机产生10个两位的正整数,存入列表ls中
ls = [random.randint(10, 99) for i in range(10)]
# 输出列表ls中的这10个随机数
print("随机数列表:", ls)
# 计算列表ls中所有数字的平均值
average = sum(ls) / len(ls)
# 统计小于平均值的数的个数
count = 0
for num in ls:
if num < average:
count += 1
# 输出平均值和小于平均值的数的个数
print("平均值:", average)
print("小于平均值的数的个数:", count)
```
程序的实现过程如下:
1. 导入random模块,用于随机产生正整数。
2. 使用列表生成式随机产生10个两位的正整数,存入列表ls中。
3. 输出列表ls中的这10个随机数。
4. 使用sum函数和len函数计算列表ls中所有数字的平均值。
5. 使用for循环遍历列表ls,统计小于平均值的数的个数。
6. 输出平均值和小于平均值的数的个数。
希望这个程序能够帮助您随机产生10个两位的正整数,存入列表中,并求出平均值和小于平均值的数的个数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)