0

I'm trying to write a program that displays the board as a list of list (the way as it is) with '_' (hidden squares), '.' (bombs), ' ' (empty squares), or '1', '2', etc.

game = {'dimensions': [2, 4],
            'board': [['.', 3, 1, 0],
                      ['.', '.', 1, 0]],
            'mask': [[False, True, False, False],
                     [False, False, False, False]],
            'state': 'ongoing'}

expected return

[['.', '3', '1', ' '],
['.', '.', '1', ' ']] 

My attempt to change it in place:

def transform(game):
    for outer in game['board']:
            for inner in outer:
                if inner ==0: 
                    inner =' '
                else: 
                    inner = "\'"+str(inner)+"\'"
                    print("inner", inner)
                    print(type(inner))
        return game['board'] 

I got print of each inner to be string in the desired format, but the final return didn't get changed at all?? :

 ...
 inner '3'
 <class 'str'>
 inner '1'
 <class 'str'>
 inner ...
  

[['.', 3, 1, 0],['.', '.', 1, 0]]
jazzfaz
  • 143
  • 1
  • 10
  • Does this answer your question? [finding and replacing elements in a list](https://stackoverflow.com/questions/2582138/finding-and-replacing-elements-in-a-list) – anatolyg Jun 30 '21 at 10:19
  • you need change `outer[{the index of inner}]`, not change `inner`. – leaf_yakitori Jun 30 '21 at 10:24
  • @anatolyg not really. i see my solution is the same as the most voted solution the question you referred. except i didn't write it in one-liner. But how come it didn't change the final results?? – jazzfaz Jun 30 '21 at 10:24

1 Answers1

1

you need change outer[{the index of inner}], not change inner.The one-liner solution which use list comprehension is to create a new list, not to change the old list.

one-liner solution:

game = {'dimensions': [2, 4],
            'board': [['.', 3, 1, 0],
                      ['.', '.', 1, 0]],
            'mask': [[False, True, False, False],
                     [False, False, False, False]],
            'state': 'ongoing'}

def transform(game):
    return [[' ' if inner == 0 else "\'"+str(inner)+"\'" for inner in outer] for outer in game['board']]

print(transform(game))

result:

[["'.'", "'3'", "'1'", ' '], ["'.'", "'.'", "'1'", ' ']]

EDIT:If you dont want use one-liner and list comprehension to create new list, you can try this code.

code:

def transform_create(game_board):
    result = []
    for board in game_board:
        temp = []
        for item in board:
            if item == 0:
                temp.append(' ')
            else:
                temp.append("\'"+str(item)+"\'")
        result.append(temp)
    return result
print(transform_create(game["board"]))

result:

[["'.'", "'3'", "'1'", ' '], ["'.'", "'.'", "'1'", ' ']]
leaf_yakitori
  • 2,232
  • 1
  • 9
  • 21
  • on a side note, if i want to create a new list, but not using one liner, is there a way to do it? (not the enumerate method) – jazzfaz Jun 30 '21 at 10:41