0

So, I've created a 7x7 matrix. And I want to change just one element of it, let's say the element on the position of [3][3], but...don't managed to find it out, how to do it properly. If I do it this way, it changes the whole column. How can I do it, to work well? Thx for help!

Here is the code:

for i in range(1, row+1):
    for j in range(1, column+1):
            board.append(0)
            matrix_1.append(board)

matrix_1[3][3]="X"

And here is the output:

0 0 X 0 0 0 0 0
0 0 X 0 0 0 0 0
0 0 X 0 0 0 0 0
0 0 X 0 0 0 0 0
0 0 X 0 0 0 0 0
0 0 X 0 0 0 0 0  
0 0 X 0 0 0 0 0
0 0 X 0 0 0 0 0

3 Answers3

0

matrix_1.append(board) should be (only) within the scope of the first for loop. So append to matrix when you exit the nested loop. Reinitialise board at each iteration of the outer loop

NomadMonad
  • 651
  • 6
  • 12
0

hope this will help you

row=7
column=7
matrix_1=[]
for i in range(1, row+1):
    board=[]
    for j in range(1, column+1):
            board.append(0)
    matrix_1.append(board)
print(matrix_1)

matrix_1[3][3]="X"
print(matrix_1)
soheshdoshi
  • 594
  • 3
  • 7
  • 24
0

maybe it will be helpfull:

 board = []
    for idx in range(4):
        board.append(['0' for _ in range(4)])

 board[0][0] = 'X'

 print(board)
Konrad
  • 1