I have a two-dimensional list with values that I changing. They need to be saved in another two-dimensional list (First list should not be changed).
I tried to set the values directly, but i'm getting this error: IndexError: list index out of range.
That's because nothing is copyied in fin_mat
.
How can i put changed values in new list?
for i, i_it in enumerate(mat):
for j, j_it in enumerate(mat[i]):
fin_mat[i][j] = mat[i-1][j] + mat[i+1][j] + mat[i][j-1] + mat[i][j+1]
UPD: Okay, I'll try to explain. Program should ask for a string, and convert it in a list that puted in another one to create a two-dimensional list:
b, st = [], [i for i in input().split()]
mat = []
it can be any long and to stop the input you must write: "end"
while (st[0] != 'end'):
st = [i for i in input().split()]
b.append(st)
if (st[0] == 'end'):
del b[-1]
Than you change string values into int
for j in b:
r = [int(item) for item in j]
mat.append(r)
print(mat)
After that, I need to created another matrix in which elements must be defined by this formula:
(i-1, j) + (i+1, j) + (i, j-1) + (i, j+1)
= fin_mat[ i ][ j ]
I can't just copy first list, and I can't change it because the values from the first list is in this formula.
I need to add values one by one in fin_mat