Reputation: 477
I have a matrix like this:
mat = [[1,2,4,5], [2,3,4,1], [4,1,1,2], [1,1,1,0]]
each cell has initially white
color associated with it. When the cell gets visited it's color changes to black
.
Now I don't know how to initialize this kind of matrix in Python. Please help me to do this.
Upvotes: 1
Views: 382
Reputation: 21923
You could subclass int
and add a color
attribute:
class Cell(int):
def __init__(self, value, color='white'):
super().__init__(value)
self.color = color
mat = [[1,2,4,5], [2,3,4,1], [4,1,1,2], [1,1,1,0]]
mat = [[Cell(i) for i in l] for l in mat]
Now you can access the color using the cell's color
attribute:
mat[y][x].color = 'black'
Upvotes: 2
Reputation: 17877
If you really want to store color names and integers in one array, you can do something like the following:
mat = [[[1, "white"], [2, "white"], [4, "white"], [5, "white"]],
[[2, "white"], [3, "white"], [4, "white"], [1, "white"]],
[[4, "white"], [1, "white"], [1, "white"], [2, "white"]],
[[1, "white"], [1, "white"], [1, "white"], [0, "white"]]]
Then you access an integer with mat[i][j][0]
and its color with mat[i][j][1]
.
I, however, would use two separate arrays: mat[i][j]
and color[i][j]
.
Upvotes: 1