def main():
def transposed(matrix):
newMatrix = []
row = []
for j in range(len(matrix[0])):
for i in range(len(matrix):
row.append(matrix[i][j])
newMatrix.append(row)
return newMatrix
main()
#When I run this code with e.g. matrix = [[1,2,3], [4,5,6], [7,8,9]]
#the output is [[1, 4, 7, 2, 5, 8, 3, 6, 9],
#[1, 4, 7, 2, 5, 8, 3, 6, 9], [1, 4, 7, 2, 5, 8, 3, 6, 9]]
所以,当我运行这段代码时,我得到一个完全错误的转置矩阵。我认为问题在于排除但我不知道自己做错了什么。
答案 0 :(得分:0)
您没有创建新行。有效的代码:
def main():
def transposed(matrix):
newMatrix = []
for j in range(len(matrix[0])):
row = []
for i in range(len(matrix):
row.append(matrix[i][j])
newMatrix.append(row)
return newMatrix
main()