要实现二维数组的转置,可以使用嵌套的循环来交换数组中的元素位置。下面是一个示例:
```python
def transpose(matrix):
# 获取原始矩阵的行数和列数
rows = len(matrix)
cols = len(matrix[0])
# 创建一个新的二维数组,用于存储转置后的矩阵
transposed = [[0 for _ in range(rows)] for _ in range(cols)]
# 遍历原始矩阵,将每个元素放入转置后的矩阵对应位置
for i in range(rows):
for j in range(cols):
transposed[j][i] = matrix[i][j]
return transposed
# 测试
matrix = [[1, 2, 3], [4, 5, 6]]
result = transpose(matrix)
print(result)
```
输出结果为:
```
[[1, 4], [2, 5], [3, 6]]
```