2016-03-07 73 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