Search code examples
pythonpython-3.xmatrixspiral

Cant understand the output of the Python 2D matrix


I am trying to print the values 1 to 9 in a Spiral Format, something like this:

[1,2,3]
[8,9,4]
[7,6,5]

However, when I try to print it through a Python script (written below), it overwrites it. I get the correct indices and the values. I just cant assign the values to the correct indices. The script is:

n = 3
res = [[0]*n]*n

    count = 1

    def getcoord(r1,c1,r2,c2):
        for c in range(c1, c2+1):
            yield r1, c
        for r in range(r1+1, r2+1):
            yield r,c2
        if r1 < r2 and c1 < c2:
            for c in range(c2-1, c1-1, -1):
                yield r2,c
            for r in range(r2-1, r1, -1):
                yield r, c1


    r1 = 0
    r2 = n-1
    c1= 0
    c2 = n-1
    val = 1
    while r1<=r2 and c1<=c2:
        for r,c in getcoord(r1,c1,r2,c2):
            print(r,c)
            print("val", val)
            res[r][c] = val
            print("res", res)
            val += 1
        r1 += 1; r2 -= 1
        c1 += 1; c2 -= 1
    return res

When I try and debug, I get the output as follows:

(0, 0)
('val', 1)
('res', [[1, 0, 0], [1, 0, 0], [1, 0, 0]])
(0, 1)
('val', 2)
('res', [[1, 2, 0], [1, 2, 0], [1, 2, 0]])
(0, 2)
('val', 3)
('res', [[1, 2, 3], [1, 2, 3], [1, 2, 3]])
(1, 2)
('val', 4)
('res', [[1, 2, 4], [1, 2, 4], [1, 2, 4]])
(2, 2)
('val', 5)
('res', [[1, 2, 5], [1, 2, 5], [1, 2, 5]])
(2, 1)
('val', 6)
('res', [[1, 6, 5], [1, 6, 5], [1, 6, 5]])
(2, 0)
('val', 7)
('res', [[7, 6, 5], [7, 6, 5], [7, 6, 5]])
(1, 0)
('val', 8)
('res', [[8, 6, 5], [8, 6, 5], [8, 6, 5]])
(1, 1)
('val', 9)
('res', [[8, 9, 5], [8, 9, 5], [8, 9, 5]])

What am I missing here? Why I cant assign the correct value to the correct index, despite having them both? Thanks in advance!


Solution

  • [0]*n creates a list containing 3 zeros: [0, 0, 0].

    Then, res = [[0]*n]*n creates a list, which you name res, that contains three times the same list we just created above.

    So, whether you refer to it by the name res[0], res[1] or res[2], you always access the same, unique list.

    Doing res[0][0] = 1 updates the first element of this list, res[2][0] = 7 updates the same element again.

    When printing res, you get three times this same list.

    What you want is three different lists. You can create them like this:

    res = [[0]*n for i in range(n)]