Python的列表还可以嵌套。
以下实例展示了3X4的矩阵列表:
以下实例将3X4的矩阵列表转换为4X3列表:
以下实例也可以使用以下方法来实现:
另外一种实现方法:
>>> transposed = []
>>> for i in range(4):
... # the following 3 lines implement the nested listcomp
... transposed_row = []
... for row in matrix:
... transposed_row.append(row[i])
... transposed.append(transposed_row)
...
>>> transposed
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]