My list looks like this:
board = [ [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
[ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
[ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
[ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
[ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
[ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
[ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
[ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0] ]
How can I modify some of the elements? Eg: the first element in each row = 0. I want to replace them with fives. So my list would look like this:
board = [ [ 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
[ 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
[ 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
[ 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
[ 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
[ 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
[ 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
[ 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0] ]
You can access a specific cell of a 2D array by doing board[r][c]
, where r is the row and c is the column. For replacing the first column with 5's, you could do:
for row in board:
row[0] = 5
This iterates over every row in board
, and sets the first item in each row to 0.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.