2016-03-07 41 views
0

這段代碼來自我製作的tic tac toe遊戲。我必須將一個值插入列表中的子列表中。這是我的嘗試,但它不起作用(Python 3.5.1)。是否可以使用insert()將值插入子列表中,如果是這樣,您如何執行此操作?將值插入列表中的子列表。 PYTHON

game_board_lst = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] 

play1_row = int(input("Player 1: What row? ")) 

play1_col = int(input("Player 1: What column? ")) 

game_board_lst.insert((play1_row - 1)(play1_col - 1), 1) 

回答

0

使用下標運算符lst[index]

既然你有一個列表的列表,你可以使用lst[row][col]

>>> game_board_lst = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] 
>>> game_board_lst[1][1]=3 
>>> game_board_lst 
[[0, 0, 0], [0, 3, 0], [0, 0, 0]] 
0

根據你的榜樣,我想你會想修改現有的值,如:

game_board_lst[play1_row-1][play1_col-1] = 1