4、输出一个3×5矩阵每行的平均值(元素为1~100的随机整数)。
时间: 2024-02-09 08:10:01 浏览: 72
随机数平均值计算的简单函数
好的,这是一个简单的 Python 代码实现:
```python
import random
# 生成3x5的矩阵,元素为1~100的随机整数
matrix = [[random.randint(1, 100) for j in range(5)] for i in range(3)]
# 输出矩阵
for i in range(3):
for j in range(5):
print(matrix[i][j], end=' ')
print()
# 计算每行的平均值并输出
for i in range(3):
row_avg = sum(matrix[i]) / 5
print(f"The average of row {i+1} is {row_avg}")
```
输出结果类似于:
```
99 12 84 62 52
76 26 15 23 35
64 81 27 90 98
The average of row 1 is 61.8
The average of row 2 is 35.0
The average of row 3 is 72.0
```
希望能够帮到你!
阅读全文