0

I have the following python function attempting to transpose an 8x8 matrix, A, store its values in the zero 8x8 matrix, B, and return B. I realize that the most readable and/or algorithmically efficient implementation would involve only two for loops, but for the sake of the larger program I'm trying to build the transpose function needs to be in a similar format to the following:

def transpose(A, B, n):
    tmp = [[0, 0, 0, 0, ]]
    for m in range(4):
        j_start = m*2
        j_finish = j_start + 2
        for k in range(4):
            i_start = k*2
            i_finish = i_start + 2
            for i in range(i_start, i_finish):
                for j in range(j_start, j_finish):
                    tmp = A[j][i]
                    B[i][j] = tmp
                    print "At B[" + str(i) + "][" + str(j) + "] = " + str(A[j][i])
    print B
    return B

When I print the value at each iteration of the innermost for loop, it prints out the correct value (which you can verify at home):

At B[0][0] = 1
At B[0][1] = 9
At B[1][0] = 2
At B[1][1] = 10
At B[2][0] = 3
At B[2][1] = 11
....
At B[6][7] = 63
At B[7][6] = 56
At B[7][7] = 64

but the ensuing print and return statements yield

[[8, 16, 24, 32, 40, 48, 56, 64],
[8, 16, 24, 32, 40, 48, 56, 64],
[8, 16, 24, 32, 40, 48, 56, 64],
[8, 16, 24, 32, 40, 48, 56, 64],
[8, 16, 24, 32, 40, 48, 56, 64],
[8, 16, 24, 32, 40, 48, 56, 64],
[8, 16, 24, 32, 40, 48, 56, 64],
[8, 16, 24, 32, 40, 48, 56, 64]]

What's the reason for the disparity between printed values from within the loop and those that are returned?

Adam Freymiller
  • 1,929
  • 7
  • 27
  • 49
  • 1
    What are the values of `A` and `B`? I think this is the situation [Python list of lists, changes reflected across sublists unexpectedly](http://stackoverflow.com/q/240178) – Bhargav Rao May 08 '16 at 04:25
  • My bad, initial input of A = [[1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16], [17, 18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31, 32], [33, 34, 35, 36, 37, 38, 39, 40], [41, 42, 43, 44, 45, 46, 47, 48], [49, 50, 51, 52, 53, 54, 55, 56], [57, 58, 59, 60, 61, 62, 63, 64]], B was an 8x8 array of 0s – Adam Freymiller May 08 '16 at 04:26
  • How did you construct your `B`? – Amadan May 08 '16 at 04:26
  • 1. b = [0, 0, 0, 0, 0, 0, 0, 0] 2. B = [b, b, b, b, b, b, b, b] – Adam Freymiller May 08 '16 at 04:27
  • 1
    There we go, shared references. The linked question is a perfect duplicate. – Amadan May 08 '16 at 04:28
  • Is it just a regular list of 64 0s then? – Adam Freymiller May 08 '16 at 04:28
  • 1
    Read the linked question. tl;dr: your `A` doesn't consist of 8x8 independent values, it consists of eight references to the same 1x8 array: you only have 8 independent values. If you change `B[3,3]`, it changes `b[3]`, which is repeated seven more times in your `B`. See [this visualisation](http://www.pythontutor.com/visualize.html#code=x+%3D+%5B%5B0%5D+*+3%5D+*+3%0Ay+%3D+%5B%5B0%5D+*+3+for+i+in+range\(0,3%29%5D%0Ax%5B2%5D%5B2%5D+%3D+1%0Ay%5B2%5D%5B2%5D+%3D+1&mode=display&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&textReferences=false&py=2&rawInputLstJSON=%5B%5D&curInstr=7). – Amadan May 08 '16 at 04:29
  • Err, I meant your `B`, and `B[3][3]`, instead of `A` and `B[3,3]`. Sorry for carelessness. But the point is, notice how different the structures for `x` and `y` are in the visualisation. – Amadan May 08 '16 at 04:37

0 Answers0