Suppose the
board = [[1, 0, 0], [0, 2, 0], [0, 0, 0]]
and I want to have a function that print the board in a readable version which would be like
new_board = [['X', ' ', ' '], [' ', 'O', ' '], [' ', ' ', ' ']]
This method does not change the actual board. It prints 'X' for 1, 'O' for 2, and empty space (' ') for 0.
Advertisement
Answer
You can use a translation dictionary and a nested list comprehension:
>>> symbols = {1: 'X', 2: 'O', 0: ' '}
>>> [[symbols[i] for i in row] for row in board]
[['X', ' ', ' '], [' ', 'O', ' '], [' ', ' ', ' ']]
Note that you can also use a string instead of a dictionary as @jasonharper mentioned but it will only work for this case, where keys start with 0 and are sequential:
>>> symbols = ' XO'
