[Python] Processing Python 2D list with for loops [duplicate]

Discussão em 'Python' iniciado por Stack, Dezembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    I wrote the following code:

    Test=[[0.0]*4]*4
    for r in range(4):
    for m in range(4):
    Test[r][m] = m+r
    print(r,m,Test[r][m])
    print(Test)


    Why finally all rows within the list contain the values from the last "r" iteration, i.e.:

    [[3, 4, 5, 6], [3, 4, 5, 6], [3, 4, 5, 6], [3, 4, 5, 6]]


    instead of

    [[0. 1. 2. 3.], [1. 2. 3. 4.], [2. 3. 4. 5.], [3. 4. 5. 6.]]


    which is generated when numpy 2D array is used?

    Continue reading...

Compartilhe esta Página