0

I want to get transpose of matrix B without using Numpy. When I use 'append' to add a new element to the list, the one that has added before also change. How can I fix it?

from decimal import *

B = [[1,2,3,5], 
     [2,3,3,5], 
     [1,2,5,1]]

def shape(M):
    r = len(M)
    c = len(M[0])
    return r,c

def matxRound(M, decPts=4):
    for p in M:
        for index in range(len(M[0])):
            p[index] = round(p[index], decPts)

def transpose(M):
    c_trans, r_trans = shape(M)
    new_row = [0]*c_trans
    trans_M = []
    for i in range(r_trans):
        for j in range(c_trans):
            new_row[j] = M[j][i]
        print 'new_row',new_row
        print 'trans_M before append',trans_M
        trans_M.append(new_row)
        print 'trans_M after append',trans_M
    return trans_M

print transpose(B)

The output is here:

new_row [1, 2, 1]
trans_M before append []
trans_M after append [[1, 2, 1]]
new_row [2, 3, 2]
trans_M before append [[2, 3, 2]]
trans_M after append [[2, 3, 2], [2, 3, 2]]
new_row [3, 3, 5]
trans_M before append [[3, 3, 5], [3, 3, 5]]
trans_M after append [[3, 3, 5], [3, 3, 5], [3, 3, 5]]
new_row [5, 5, 1]
trans_M before append [[5, 5, 1], [5, 5, 1], [5, 5, 1]]
trans_M after append [[5, 5, 1], [5, 5, 1], [5, 5, 1], [5, 5, 1]]
[[5, 5, 1], [5, 5, 1], [5, 5, 1], [5, 5, 1]]
Martin
  • 89
  • 1
  • 7

2 Answers2

0

I will complete @glibdud comment's answer :

What you are doing now is creating a list that fits your needs for your Transpose. You are creating your new matrix.

You are, then, appending your transposed value into your new matrix... without creating a new Transpose list. What happens then is that you modify the last list you just appended, and try to append it again.

So in the end, you added the 4 same lists to your new matrix. As the 4 lists point to the same address in memory as they are the same object, your new matrix have 4 identical rows.

IMCoins
  • 3,149
  • 1
  • 10
  • 25
  • Yes, I know the reason why this problem appears. I just want to know how to fix it. – Martin Dec 06 '17 at 15:03
  • I don't think you **really** understand why this problem appears, or else you would be able to fix it. I just told you what to do : Create a list, append it. Create a list, append it. Here, you are just creating a list, appending it, **modifying it**, appending the same one, and so on. You don't want a full direct answer, right ? – IMCoins Dec 06 '17 at 15:11
  • I appreciate your answer. I put new_row = [0]*c_trans in i for-loop and fix this problem. Thank you very much. – Martin Dec 06 '17 at 23:08
  • You might want to consider closing this post so others will know the solution to their problem is here. – IMCoins Dec 06 '17 at 23:34
0

The most pythonic way I know to perform matrix transposition without using Numpy (that should be the preferred way), is by using list unpacking (list expansion) and the builtin zip function transposed = list(zip(*B)).

However, zip() return tuples while your original matrix is a list of lists. So, if you want to keep your structure, you can use transposed = [list(i) for i in zip(*B)]

alec_djinn
  • 10,104
  • 8
  • 46
  • 71