I'm trying to convert this maze:
[['#', '#', '#', '#', '#', '#'],
['#', 'o', '#', ' ', '#', '#'],
['#', ' ', '#', ' ', '#', '#'],
['#', ' ', ' ', 'x', '#', '#'],
['#', '#', '#', '#', '#', '#']]
to this:
[[0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 0],
[0, 1, 0, 1, 0, 0],
[0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0]]
Is there a quick way to do convert the char values to ints? I don't know exactly what I need to be searching for. Also, what is the best way to traverse this?
CodePudding user response:
matrix = [['#', '#', '#', '#', '#', '#'],
['#', 'o', '#', ' ', '#', '#'],
['#', ' ', '#', ' ', '#', '#'],
['#', ' ', ' ', 'x', '#', '#'],
['#', '#', '#', '#', '#', '#']]
for i in range(len(matrix)):
matrix[i] = list(map(lambda x: int(x != '#'), matrix[i]))
print(matrix)
CodePudding user response:
t = [['#', '#', '#', '#', '#', '#'],
['#', 'o', '#', ' ', '#', '#'],
['#', ' ', '#', ' ', '#', '#'],
['#', ' ', ' ', 'x', '#', '#'],
['#', '#', '#', '#', '#', '#']]
In 1 line
x = [[1 if a != "#" else 0 for a in p] for p in t]
Result
print(DataFrame(x))
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 1 0 1 0 0
2 0 1 0 1 0 0
3 0 1 1 1 0 0
4 0 0 0 0 0 0