需要学习资源,请点击开挂教学楼
In current case for the code below temp = [[['#', '#', '.'], ['#', '.', '.'], ['.', '.', '.']], [['#', '#', '.'], ['#', '.', '.'], ['.', '.', '.']], [['#', '#', '.'], ['#', '.', '.'], ['.', '.', '.']], [['#', '#', '.'], ['#', '.', '.'], ['.', '.', '.']]]
and grid_row//2 = 2
.
temp
undergoes a nested for-loop to make each row(a list inside a list inside temp) in temp[0]
equal to that row plus it's corresponding row in temp[2]
. The same applies on temp[1]
from temp[3]
.
The problem is that all lists inside temp
are being modified. I have tried the code below alone and it works as expected; but when I place it in the function defined below, it acts abnormally. Why? And how can I fix this?
grid_row = len(image)
print(temp)
for sqr_ind in range(grid_row//2, len(temp)):
for sqr_row in range(3):
temp[sqr_ind % (grid_row//2)][sqr_row] += temp[sqr_ind][sqr_row]
print(temp)
Here is the function, and please note that the current itr
is equal to 1.
def enh(rules_0, rules_1, itr):
image = [['#', '.', '.', '#'], ['.', '.', '.', '.'], ['.', '.', '.', '.'], ['#', '.', '.', '#']]
while itr > 0:
temp = []
if len(image) % 2 == 0:
#Extracted long code to construct "temp"
ln_img = len(image)
for sqr_ind in range(ln_img//2,len(temp)):
for sqr_row in range(3):
temp[sqr_ind%(ln_img//2)][sqr_row] += temp[sqr_ind][sqr_row]
image = []
for sqr_ind in range(ln_img//2):
for sqr_row in range(3):
image.append(temp[sqr_ind][sqr_row])
elif len(image) % 3 == 0:
#Extracted long code to construct "temp"
ln_img = len(image)
for sqr_ind in range(ln_img//3,len(temp)):
for sqr_row in range(4):
temp[sqr_ind%(ln_img//3)][sqr_row] += temp[sqr_ind][sqr_row]
image = []
for sqr_ind in range(ln_img//3):
for sqr_row in range(4):
image.append(temp[sqr_ind][sqr_row])
itr -= 1
# Extracted other Not relative code