12-矩阵1_转置

  • 阅读: 240
  • 更新: 2022-06-11

难度:

1. 题目描述

2. 关键点

  • for 循环

3. 代码实现

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
jz1 = [
    [1, 2, 3],
    [4, 5, 6],
]
jz1_row_len = len(jz1)
jz1_col_len = len(jz1[0])

jz2 = []
jz2_row_len = jz1_col_len
jz2_col_len = jz1_row_len

for i in range(jz2_row_len):
    row = []
    for j in range(jz1_row_len):
        row.append(jz1[j][i])
    jz2.append(row)

print(f'源矩阵为:{jz1}')
print(f'转置矩阵为:{jz2}')

4. 运行示例

1
2
源矩阵为:[[1, 2, 3], [4, 5, 6]]
转置矩阵为:[[1, 4], [2, 5], [3, 6]]

5. 进阶思考

  1. 可输入任意矩阵

=== 全文完 ===


欢迎加入QQ群:778391413

京公网安备 11011302003970号 京ICP备2022012301号-1