Skip to content Skip to sidebar Skip to footer

How Do I Replace A Specific String In A 2d Array?

I'm making a program that Identifies if a blank tile exists or not. I already have a code in my 2d array which is arr2 = [['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0'

Solution 1:

You have to enumerate your array to change the value at a specific index.

for i, row in enumerate(arr2):
    for j, cell in enumerate(row):
        ifcell== '#':
            arr2[i][j] = 'A'

Solution 2:

You are replacing the contents of the variable i, not the element of the array from which it came.

Solution 3:

you have to enumerate the data instead of just looping over it so that you can get the index of the item and replace it.

for xindex, x in enumerate(arr2):
    for yindex, y in enumerate(x):
        ify== "#":
            arr2[xindex][yindex] = "A"

Post a Comment for "How Do I Replace A Specific String In A 2d Array?"