在Python中为NxN矩阵转置创建结果矩阵时感到困惑。

huangapple go评论64阅读模式
英文:

Confused about creating a result matrix for NxN matrix transposition in Python

问题

NxN矩阵转置在将"result"矩阵设置为输入矩阵时会产生错误答案。

def flippingMatrix(matrix):
    result = [[0 for i in range(len(matrix))] for j in range(len(matrix))]
    for i in result:
        print(i)
    
    for i in range(len(matrix)):
        for j in range(len(matrix)):
            result[j][i] = matrix[i][j]
    
    for i in result:
        print(i)
            

# //主程序//
n = int(input().strip())

matrix = []

for i in range(n):
    matrix.append(list(map(int, input().rstrip().split())))

result = flippingMatrix(matrix)

现在,如果运行此程序,我们输入'N',即行和列的数量。然后在下一行输入矩阵元素。这是我提供的输入:

2
1 2
3 4

输出结果:

[0, 0]     # 这是我创建的结果矩阵
[0, 0]
[1, 3]     # 最终的结果矩阵
[2, 4]

问题在于:如果我将

"result=matrix"

而不是

result=[[0 for i in range(len(matrix))] for j in range(len(matrix))]

我会得到错误的答案。我假设我们只需要一个NxN矩阵的布局作为结果矩阵。那么为什么将结果设置为输入会导致错误答案呢?反正它最终都会被覆盖。

对于与之前相同的输入,我得到以下输出:

[1, 2]     # 初始结果矩阵,即复制的输入矩阵
[3, 4]
[1, 2]     # 最终的结果矩阵
[2, 4]

如果需要进一步解释,请留言在这个帖子上,我将尽力解释我的问题。

编辑:
我在建议进行编辑后使用的新代码如下,我仍然面临相同的问题。

def flippingMatrix(matrix):
    result = matrix
    for i in result:
        print(i)
    
    for i in range(len(matrix)):
        for j in range(len(matrix)):
            result[j][i] = matrix[i][j]
    
    return result

# //主程序//
n = int(input().strip())

matrix = []

for i in range(n):
    matrix.append(list(map(int, input().rstrip().split())))

result = flippingMatrix(matrix)
print(result)

这里的问题可能是赋值语句 result = matrix。但我认为结果矩阵不需要为零才能被重写。

英文:

NxN Matrix transposing gives wrong answers upon making "result" matrix = input matrix

Novice programmer here. I am trying to transpose a NxN matrix. Putting the code here to make the problem clear:

def flippingMatrix(matrix):
    result=[[0 for i in range(len(matrix))] for j in range(len(matrix))]
    for i in result:
        print(i)
    
    for i in range(len(matrix)):
        for j in range(len(matrix)):
            result[j][i]=matrix[i][j]
    
    for i in result:
        print(i)
            

# //Main Program//
n = int(input().strip())

matrix = []

for i in range(n):
    matrix.append(list(map(int, input().rstrip().split())))

result = flippingMatrix(matrix)

Now, if we run this program, we input 'N' which is no. of rows and columns. And in the next line, elements into the matrix. This is the input that I gave:

2

1 2

3 4

The output:

[0, 0]     //This is the result matrix that I made//
[0, 0]
[1, 3]     //The final result matrix//
[2, 4]

Coming to the problem: If, I assign the

"result=matrix" 

instead of

result=[[0 for i in range(len(matrix))] for j in range(len(matrix))]

I get the wrong answer. I am assuming that we need a result matrix as only a layout of NxN matrix. Then why does making the result same as input gives wrong answer? It is anyways going to be overwritten.

With the same input as earlier, I get the following output:

[1, 2]     //Initial result matrix which is the copied input matrix//
[3, 4]
[1, 2]     //The final result matrix//
[2, 4]

I apologize if the problem is a bit unclear, I tried my best. If anyone needs further explanation, comment on this post and I will try to explain my problem

EDIT:
The new code that I am using after I was suggested an edit. The new code is as follows and I am facing the same problem

def flippingMatrix(matrix):
result=matrix
for i in result:
    print(i)

for i in range(len(matrix)):
    for j in range(len(matrix)):
        result[j][i]=matrix[i][j]

return result
        

# //Main Program//
n = int(input().strip())

matrix = []

for i in range(n):
    matrix.append(list(map(int, input().rstrip().split())))

result = flippingMatrix(matrix)
print(result)

Now the problem here might be because of the assignment statement result = matrix. But I don't think result matrix should be zero in order to be rewritten.

答案1

得分: 0

你的函数应该有一个返回值。如果你在函数内部分配了 matrix=result,它不会传播到函数外部,这只是一个引用/指针分配。每个矩阵的值不会被复制。

尝试:

def flippingMatrix(matrix):
    result = [[0 for i in range(len(matrix))] for j in range(len(matrix))]
    for i in range(len(matrix)):
        for j in range(len(matrix)):
            result[j][i] = matrix[i][j]
    return result

# //主程序//
n = int(input().strip())

matrix = []

for i in range(n):
    matrix.append(list(map(int, input().rstrip().split())))

print(matrix)
result = flippingMatrix(matrix)
print(result)

在这里测试 n = 3:

def flippingMatrix(matrix):
    result = [[0 for i in range(len(matrix))] for j in range(len(matrix))]
    for i in range(len(matrix)):
        for j in range(len(matrix)):
            result[j][i] = matrix[i][j]
    return result

# //主程序//
n = 3
matrix = [[i + j * n for i in range(n)] for j in range(3)]
print(matrix) # 返回 [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

matrix = flippingMatrix(matrix)
print(matrix) # 返回 [[0, 3, 6], [1, 4, 7], [2, 5, 8]]
英文:

Your function should have a return, if you assign matrix=result inside its scope, this won't propagate outside of the function, it's only a reference/pointer assignment. The values of each matrix is not copied.

Try:

def flippingMatrix(matrix):
    result=[[0 for i in range(len(matrix))] for j in range(len(matrix))]
    for i in range(len(matrix)):
        for j in range(len(matrix)):
            result[j][i]=matrix[i][j]
    return result
            

# //Main Program//
n = int(input().strip())

matrix = []

for i in range(n):
    matrix.append(list(map(int, input().rstrip().split())))

print(matrix)
result = flippingMatrix(matrix)
print( result)

Tested here for n = 3:

def flippingMatrix(matrix):
    result=[[0 for i in range(len(matrix))] for j in range(len(matrix))]
    for i in range(len(matrix)):
        for j in range(len(matrix)):
            result[j][i] = matrix[i][j]
    return result
            

# //Main Program//
n = 3
matrix = [[i+j*n for i in range(n)] for j in range(3)]
print( matrix) # returns [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

matrix = flippingMatrix(matrix)
print( matrix) # returns [[0, 3, 6], [1, 4, 7], [2, 5, 8]]

答案2

得分: 0

以下是要翻译的内容:

所以我通过与 @slothrop 在评论中交流了解到,Python 不会轻易复制嵌套列表。为了创建一个独立的二维数组副本,我必须使用不同的命令,如下所示:

b = copy.deepcopy(a)
b = [item[:] for item in a]
b = [item.copy() for item in a]
b = [list(item) for item in a]
b = [copy.copy(item) for item in a]
b = []; b.extens[a]

这里是相关问题的链接:这里

英文:

So I learned by talking in comments with @slothrop, Python doesn't take copying of lists inside of lists lightly. In order to make a copy, I have to use different commands like so that it actually makes an independent 2D array:

b = copy.deepcopy(a)
b = [item[:] for item in a]
b = [item.copy() for item in a]
b = [list(item) for item in a]
b = [copy.copy(item) for item in a]
b = []; b.extens[a]

The link to such other question is here.

huangapple
  • 本文由 发表于 2023年5月22日 21:00:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定