1

So I am trying to create a grid that can have it's individual "grid squares" replaced by any given symbol. The grid works fine but it is made of lists within lists.

Here's the code

size = 49
feild = []
for i in range(size):
    feild.append([])
for i in range(size):
    feild[i].append("#")
feild[4][4] = "@" #This is one of the methods of replacing that I have tried
for i in range(size):
    p_feild = str(feild)
    p_feild2 = p_feild.replace("[", "")
    p_feild3 = p_feild2.replace("]", "")
    p_feild4 = p_feild3.replace(",", "")
    p_feild5 = p_feild4.replace("'", "")
    print(p_feild5)

As you can see that is one way that I have tried to replace the elements, I have also tried:

feild[4[4]] = "@"

and

feild[4] = "@"

The first one replaces all "#" 4 elements in from the left with "@" The second one gives the following error

TypeError: 'int' object is not subscriptable

2 Answers 2

1

The make a grid of # with row 3, column 3 replaced with @:

>>> size = 5
>>> c = '#'
>>> g = [size*[c] for i in range(size)]
>>> g[3][3] = '@'
>>> print('\n'.join(' '.join(row) for row in g))
# # # # #
# # # # #
# # # # #
# # # @ #
# # # # #
Sign up to request clarification or add additional context in comments.

2 Comments

If I do this I will get a grid entirely of "@", I just want to replace one element
@ayNONE I updated the answer to allow you to change individually any row-column.
0

May be you're looking for this :-

size = 49
feild = []
for i in range(size):
    feild.append([])
for i in range(size):
    map(feild[i].append, ["#" for _ in xrange(size)])
i = 4
feild[i][0] = "@"

6 Comments

But wouldn't create multiple "@". I just want to replace specific grid squares.
Not an issue, which ever grid square you want to replace, just specify its row index as i in feild[i][0]
This replaces the entire grid if I leave it as i. If I change i it does a whole row.
I think, it'll change specific element in your grid. Your grid is at present 49 * 1. So, to change any element you'll have to set grid[<rowNo>][0] = <val>
I have changed code. Now your grid will become of size 49 * 49.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.