1

I tried the most basic approach for matrix transpose in python. However, I don't get the required results. Following by the code:

A = [ [1, 1, 1, 1], 
    [2, 2, 2, 2], 
    [3, 3, 3, 3], 
    [4, 4, 4, 4]] 

#print (A)
def TS (A):
    B = A
    for i in (range(len(A))):
        for j in (range(len(A))):
            A[i][j] = B [j][i]
TS(A)
#print (A)

for i in range(len(A)): 
    for j in range(len(A)): 
        print(B[i][j], " ", end='') 
    print() 

This is the result I get:

1  2  3  4  
2  2  3  4  
3  3  3  4  
4  4  4  4  
5
  • Transpose matrix.... Commented Jul 9, 2019 at 13:23
  • Even after changing to B = A.copy() I still don't get the right result. Commented Jul 9, 2019 at 13:36
  • copy makes a shallow copy. You need copy.deepcopy to duplicate the content of the inner lists too. Commented Jul 9, 2019 at 13:38
  • can anyone check my answer? Commented Jul 9, 2019 at 13:43
  • I tell you what's wrong with your code. First, B = A makes every change to A is also a change for B. So after row 1 is modified in A, B is modified as well. B[0][1] is now 2 because A[1][0] was 2. In the next step when A[1][0] is to be modified, it takes 2. from B[0][1]. And so on for the rest of rows... Check my answer, you find it helpful Commented Jul 9, 2019 at 13:58

5 Answers 5

3

why do dont you try numpy :)

import numpy as np
z = np.transpose(np.array(A))
Sign up to request clarification or add additional context in comments.

2 Comments

Or without numpy: print(list(zip(*A)))
you need to do this, to avoid returning a list of tuples: list(map(list, zip(*A)))
1

Your problem is two fold:

1- B was a label on matrix A, that is every modification to A, also modified B
2- B was local to the transpose function, and could not be accessed outside

A = [[1, 1, 1, 1], 
     [2, 2, 2, 2], 
     [3, 3, 3, 3], 
     [4, 4, 4, 4]] 

def TS (A):
    B = [row[:] for row in A]   # make a copy of A, not assigning a new label on it.
    for i in (range(len(A))):
        for j in (range(len(A))):
            B[i][j] = A[j][i]
    return B

B = TS(A)

for i in range(len(A)): 
    for j in range(len(A)): 
        print(B[i][j], " ", end='') 
    print() 

output:

1  2  3  4  
1  2  3  4  
1  2  3  4  
1  2  3  4 

3 Comments

Would B = A[:][:] be as same as B = [row[:] for row in A] ?
Thank you @MadPhysicist, yes, that explains it well
No, that would not work, the outer list would be copied (twice), but the inner lists in B would still be references to the same inner lists in A - the nested references would not be copied, try it: B = A[:][:], A[1][1] = 99, print(B)
1
A = [ [1, 1, 1, 1], 
    [2, 2, 2, 2], 
    [3, 3, 3, 3], 
    [4, 4, 4, 4]]

def transpose(A,B): 

    for i in range(len(A)): 
        for j in range(len(A)): 
            B[i][j] = A[j][i]    

B = [[0 for x in range(len(A))] for y in range(len(A))]  

transpose(A, B) 

print("Result matrix is") 
for i in range(len(A)): 
    for j in range(len(A)): 
        print(B[i][j], " ", end='') 
    print() 

Output

Result matrix is

1  2  3  4
1  2  3  4
1  2  3  4
1  2  3  4

1 Comment

Please correct my own code and find what's wrong there.
1

Copy A to B using deepcopy then it should be B [i][j] = A [j][i]. Must be a typo error.

A = [[1, 1, 1, 1], 
    [2, 2, 2, 2], 
    [3, 3, 3, 3], 
    [4, 4, 4, 4]] 

#print (A)
def TS (A):
    from copy import deepcopy
    B = deepcopy(A)
    for i in (range(len(A))):
        for j in (range(len(A))):
            B[i][j] = A [j][i]
    return B
B = TS(A)
#print (len(A))

for i in range(len(B)): 
    for j in range(len(B)): 
        print(B[i][j], " ", end='') 
    print() 

Result:

1  2  3  4  
1  2  3  4  
1  2  3  4  
1  2  3  4 

Comments

0

B was a label on matrix A, that is every modification to A, also modified B. Hence the wrong values from second row. Why don't you try it like this...

 A = [ [1, 1, 1, 1], 
 [2, 2, 2, 2], 
 [3, 3, 3, 3], 
 [4, 4, 4, 4]] 
 def TS(A):
    for i in range(len(A)): 
        for j in range(len(A)): 
            print(A[j][i], " ", end='') 
        print()        
 TS(A)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.