1

Here was my original code:

A=[[0,0,1,1],[1,0,1,0],[1,1,0,0]]

rowflags=[1,0,0]
b=A

#code below changes b[0] to [1, 1, 0, 0]
for rownum in range(3):
    if rowflags[rownum]==1:
        for colnum in range(4):
            if b[rownum][colnum]==1:b[rownum][colnum]=0
            else:b[rownum][colnum]=1
print(A)
print(b)
print(b is A)

It gave the output:

[[1, 1, 0, 0], [1, 0, 1, 0], [1, 1, 0, 0]]
[[1, 1, 0, 0], [1, 0, 1, 0], [1, 1, 0, 0]]
True

Stackoverflow suggested changing b=A to b=A.copy() or b=A[:] but both yield this odd output:

[[1, 1, 0, 0], [1, 0, 1, 0], [1, 1, 0, 0]]
[[1, 1, 0, 0], [1, 0, 1, 0], [1, 1, 0, 0]]
False

I tried a simplified example which worked fine:

A=[[0,0,1,1],[1,0,1,0],[1,1,0,0]]

rowflags=[1,0,0]
b=A[:]

#code below changes b[0] to [1, 1, 0, 0]
b[0]=[1, 1, 0, 0]

print(A)
print(b)
print(b is A)

Giving the output:

[[0, 0, 1, 1], [1, 0, 1, 0], [1, 1, 0, 0]]
[[1, 1, 0, 0], [1, 0, 1, 0], [1, 1, 0, 0]]
False
AndrewGraham
  • 310
  • 1
  • 8

0 Answers0