Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

python - 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' '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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...