This really, really messed with me today. This is the most minimum working example I could get:
array = [ 1, 2, 1, 3, 4 ]
n = 3
strips = [[0] * n] * n
mSum = 7
j = 0
strips[0][j] = mSum
print(f'before strips[0]: {strips[0][0]}')
for i in range(1, len(array) - n + 1):
mSum += array[i + n - 1] - array[i - 1]
strips[i][j] = mSum
print(f'strips[{i}][{j}]: {strips[i][j]}')
print(f'after strips[0]: {strips[0][0]}')
If strips
is not a nested-list, the error doesn't occur. Here is the output:
before strips[0][0]: 7
strips[1][0]: 9
strips[2][0]: 11
after strips[0][0]: 11
Why is strips[0][0]
different before and after the loop (where it does not get modified)? The only thing I can think of is that it's somehow connected to mSum
.
The same code in a language like C++ does not have this issue.