I have a list containing n identical 3x3 matrices, where n is the number of zeros in one such matrix.
In my first matrix, I want to replace the first 0 with the integer 2. In the second matrix, I want to replace the second 0 with 2. This goes on until the n:th matrix where I want to replace the n:th 0 with 2. I.e. every matrix should have only one index replaced.
My code so far:
def replaceZero(x):
# Omitted code where I count number of 0 in x
n = 9 # in this example
# Creating a list which holds n matrices
lists = []
for i in range(0,n):
lists.append(x)
# Replacing the i:th 0 with 2
for i in range(0,n):
m = -1
for j in range(0,3):
for k in range(0,3):
if lists[i][j][k] == 0:
m = m + 1
if m == i:
lists[i][j][k] = 2
return lists
# Example of a matrix
x = [[0,0,0],[0,0,0],[0,0,0]]
I can't figure out why it doesn't work. Although, too many zeros are replaced and the replacement is identical for each matrix.
I am trying to increment m with 1 each time a zero is found. Only when m has the same value as the index of the current matrix should the replacement occur.