0

So, I try to create a program that use a board created with lists inside a list :

[[0,0,0,...,L], [0,0,0,...,L], [0,0,0,...,L], [0,0,0,...,L],...,H]]

Example with x = 4 and y = 2 ( x = H, y = L ) :
>> [[0,0,0,0],[0,0,0,0]]

My code is :

...
#>> Initialistation
board = []
temp = []

H = int(input('height : '))
L = int(input('lenght : '))

for i in range(0,L):
    temp.append('0')

for j in range(0,H):
    board.append(temp)

temp_x = int(input('x :'))
temp_y = int(input('y :'))
board[temp_y[temp_x]] = 'f' # line 29

print(board)

Basically, I would add value in my board, but anything I've tried not working as I would :

My Result:

# with the example above
board[0][1] = 'f'
>> [[0,0,'f',0],[0,0,'f',0]

Excpected Result:

# with the example above
board[0][1] = 'f'
>> [[0,0,0,0],[0,0,'f',0]

What i have tried:

board[[0][1]] = 'f'
>> IndexError (line 29): list index out of range
board([0][1]) = 'f'
>> SynthaxError (line 29) : can't assign function call
board[p0[1]] = 'f'
>> TypeError (line 29): 'int' object is not subscriptable

The problem is that 'f' is write in the board each line and not only the expected one So How can I get the expected result ?

  • What is the expected output ? – Piou44 Jun 25 '19 at 07:35
  • Possible duplicate of [How to define a two-dimensional array in Python](https://stackoverflow.com/questions/6667201/how-to-define-a-two-dimensional-array-in-python) – Georgy Jun 25 '19 at 07:56

2 Answers2

2

What you want should work like:

a  = [[0,0,0,0],[0,0,0,0]]

a[1][2] = 'f'

print(a)

Output:

[[0, 0, 0, 0], [0, 0, 'f', 0]]

EDIT

Now the problem is clear. You get that behaviour because of how you create your board. Every row in your board references to the same list (temp). So if you think you change one row you change all of them because you change the value all rows reference to.

You can change the creation of your board to either a oneliner (list comprehension):

board = [['0' for i in range(L)] for k in range(H)]

Or a nested Loop:

board = []
for j in range(0,H):
    temp = []
    for i in range(0,L):
        temp.append('0')

    board.append(temp)

Your complete code with changes:

H = int(input('height : '))
L = int(input('lenght : '))

board = [['0' for i in range(L)] for k in range(H)]

temp_x = int(input('x :'))
temp_y = int(input('y :'))

print(temp_x)
print(temp_y)

board[temp_y][temp_x] = 'f' # line 29

print(board)

produces the output:

height : 2
lenght : 2
x :1
y :1
1
1
[['0', '0'], ['0', 'f']]
Florian H
  • 3,052
  • 2
  • 14
  • 25
  • Try to write what you have (your list before) and what you expect to be the output (list after the wanted line of code) – Florian H Jun 25 '19 at 07:33
  • My list before is `[[0,0,0,0],[0,0,0,0]]` then I got `[[0,0,'f',0],[0,0,'f',0]` and I expected `[[0,0,'f',0],[0,0,0,0] –  Jun 25 '19 at 07:36
  • @Edhyjox, I think Florian's answer will work for you, just change `a[0][2]='f'` – Lê Tư Thành Jun 25 '19 at 07:39
0

I finally found the awnser :

the problem is coming at line board.append(temp), baceause it's appending litterally temp as 'the list list temp' and not at '[0,0,0,0] !

The way to solve this is to ungroup temp with '*' and regroup it as a list with '[]' So the line : board.append(temp) should be : board.append([*temp])