python 编写程序,对一个4*4的矩阵进行随机赋值,然后对该矩阵进行转置,并输出转置后的结果
时间: 2023-04-03 21:03:33 浏览: 359
矩阵转置程序
可以使用以下代码实现:
```python
import random
# 生成4*4的矩阵并随机赋值
matrix = [[random.randint(1, 10) for j in range(4)] for i in range(4)]
# 输出原始矩阵
print("原始矩阵:")
for row in matrix:
print(row)
# 对矩阵进行转置
transpose_matrix = [[matrix[j][i] for j in range(4)] for i in range(4)]
# 输出转置后的矩阵
print("转置后的矩阵:")
for row in transpose_matrix:
print(row)
```
输出结果如下:
```
原始矩阵:
[7, 6, 10, 2]
[3, 6, 7, 10]
[7, 6, 4, 9]
[5, 10, 2, 3]
转置后的矩阵:
[7, 3, 7, 5]
[6, 6, 6, 10]
[10, 7, 4, 2]
[2, 10, 9, 3]
```
阅读全文