[Python] Python updating lists error! List will not update inside while loop

Discussão em 'Python' iniciado por Stack, Outubro 5, 2024.

  1. Stack

    Stack Membro Participativo

    grid_height = 4
    grid_width = 5
    cell = [0, 1]
    OFFSETS = (1, 0)

    col = []

    while (cell[0] < grid_height) and (cell[1] < grid_width):
    print cell
    col.append(cell)
    cell[0] += OFFSETS[0]
    cell[1] += OFFSETS[1]

    print col


    For this Python code, I try to get update the "cell" list and store every single new value to the list "col". However, if you run this code in Python, the value of the "cell" that is printing out is exactly the value I want to have, but when I print out "col", it just keeps repeating the value of the last, unwanted, out-of-range value of "cell". I don't think this is a scope problem as while() does not create a new scope in Python.

    Continue reading...

Compartilhe esta Página