Reputation: 31
I'm making a replica of minesweeper in pygame, and I am trying to make a matrix to keep track of the bombs, but I do not understand a certain matrix definition that I saw online.
I've seen code online that does the following to define a matrix filled completely with zeroes:
def create_table(n):
return [[0] * n for i in range(n)]
Where n is the number of rows and columns in the matrix.
I don't understand how [0] * n
produces, for example if n=3
, [0, 0, 0].
Upvotes: 1
Views: 43
Reputation: 1383
[0] * 3
is basically just [0] + [0] + [0]
. A similar example that might be clearer:
[1, 2, 3] + [4, 5] == [1, 2, 3, 4, 5]
Depending on how you want your matrix to be laid out (either matrix[row][column]
or matrix[column][row]
), you have to replace the n
s by n_columns
and n_rows
.
Upvotes: 1