How do I replace a specific string in a 2d array?

2024/11/18 18:17:36

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' '0']['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']['0' '0' '0' '0' '0' 'E' 'A' '#' 'L' 'E' '0' '0' '0' '0' '0']['0' '0' '0' '0' '0' '0' 'P' '0' '0' '0' '0' '0' '0' '0' '0']['0' '0' '0' '0' '0' '0' 'P' 'E' 'A' 'K' '0' '0' '0' '0' '0']['0' '0' '0' '0' '0' '0' 'L' '0' '0' '0' '0' '0' '0' '0' '0']['0' '0' '0' '0' '0' '0' 'E' '0' '0' '0' '0' '0' '0' '0' '0']['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']]

I want to replace the # which is I labeled for the blank tile into one of the letters in the alphabet(A-Z). I already made a code that replaces the blank tile.

for i in arr2:for j in i:if j == '#':i = [j.replace('#', 'A')]

But for some reason it is still a # not an A. How do I replace the #(blank tile) in the given array into an alphabet? Also, how do I make a pop-up message so that players can just type an alphabet to replace for the # which is the blank tile?

Answer

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):if cell == '#':arr2[i][j] = 'A'
https://en.xdnf.cn/q/120045.html

Related Q&A

from _dlib_pybind11 import * ModuleNotFoundError: No module named _dlib_pybind11

I actually working on a face recognition project but getting an error such as: from _dlib_pybind11 import * ModuleNotFoundError: No module named _dlib_pybind11Please help Ill appreciate any bits of hel…

Sudoku with user input

I am trying to write a Sudoku game with user input. So the user can choose what row/column it wants to and what number. I have to import it from a text file and have made a save and load function. I ha…

Move user to a textchannel as soon as he has pressed a button

I have a problem. I would like to move a user from one specific textchannel to another specific textchannel as soon as he has pressed a button. Unfortunately I get an error. class MyView(discord.ui.Vie…

scrape the about page of websites with Python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Tkinter entry widget execution [duplicate]

This question already exists:Entry widget in tkinterClosed 2 years ago.So I made a simple program however it doesnt seem to work my code is: e = Entry(root, font = 20,borderwidth=5)e.grid(row=1)def cap…

Python Redefine the variable given as a parameter to the function

Hello I am trying to make theme window with tkinter. There is 5 variable for different widgetss color. I will use color dialog for choosing colors but I dont want to define 5 functions. So I think I ca…

Need a Gui Keypad for a touchscreen that outputs a pin when code is correct

I have a raspberry pi with a touchscreen running raspbian, Im hoping to have a Gui on the touchscreen that had a number keypad that when a correct input is entered a pin will output to a door latch or …

How can i make the python to wait till i complete speaking?

I am writing a program to recognise the speech from a microphone and the code will process accordingly. The code I wrote for this purpose is below.import speech_recognition as sr import webbrowser impo…

Facing AttributeError: list object has no attribute lower

I have posted my sample train data as well as test data along with my code. Im trying to use Naive Bayes algorithm to train the model.But, in the reviews Im getting list of list. So, I think my code is…

why I cannot use max() function in this case? [duplicate]

This question already has answers here:Why do I get "TypeError: int object is not iterable" when trying to sum digits of a number? [duplicate](4 answers)Closed 1 year ago.n,m,k=map(int, inpu…