.不用numpy创建一个从1到 80、步长为2的一维数组,并将其变成一个8x5的二维矩 阵,转置后输出。要求:每一步操作单独成行。
时间: 2024-05-02 10:20:56 浏览: 86
```python
arr = list(range(1, 81, 2)) # 创建一维数组
matrix = [] # 初始化二维矩阵
for i in range(0, len(arr), 5):
matrix.append(arr[i:i+5]) # 将一维数组按每5个元素分割成8个小数组,组成二维矩阵
print(matrix) # 输出二维矩阵
matrix_T = [] # 初始化转置后的二维矩阵
for i in range(5):
row = [] # 初始化每一列
for j in range(8):
row.append(matrix[j][i]) # 将每一列的元素添加到对应的行中
matrix_T.append(row) # 添加行到转置后的二维矩阵中
print(matrix_T) # 输出转置后的二维矩阵
```
输出结果:
```
[[1, 3, 5, 7, 9], [11, 13, 15, 17, 19], [21, 23, 25, 27, 29], [31, 33, 35, 37, 39], [41, 43, 45, 47, 49], [51, 53, 55, 57, 59], [61, 63, 65, 67, 69], [71, 73, 75, 77, 79]]
[[1, 11, 21, 31, 41, 51, 61, 71], [3, 13, 23, 33, 43, 53, 63, 73], [5, 15, 25, 35, 45, 55, 65, 75], [7, 17, 27, 37, 47, 57, 67, 77], [9, 19, 29, 39, 49, 59, 69, 79]]
```
阅读全文