How do I replace characters in a list of strings with a different character?

Question:

I am making a platformer with enemies and want to have enemies randomly placed on the map. My map contains lists of strings that specify the map tiles. Basically I just want to randomly put the letter ‘E’ on a random places in the list but only in areas that are empty.

My code:

import random

old_level_map = [
'6--------------------------------------------------------------------7',
'L                                                                    I',
'9____________________________________________________________________8',
'                                                                      ',
'                                                                      ',
'      P                                                               ',
'                                                                      ',
'                                                                      ',
'6--------------------------------------------------------------------7',
'L                                                                    I',
'9____________________________________________________________________8']

level_map = []
for j in old_level_map:
    line = list(j)
    for i in line:
        if str.isspace(i) == True:
            if random.random() < 0.1:
                new = i.replace(i, 'E')
                i = new
        print(i)
    level_map.append(line)

I might have went about this wrong. I went through each item in the list and then created individual lists out of those. Then I went through those lists and checked if there was an empty space, then there was a 10% chance of the item in the list being replaced by an ‘E’. When I ran my code, no enemies showed up. So I printed ‘i’ and it showed that the letter ‘E’ was scattered throught. This means that ‘i’ doesn’t correspond to my line list anymore. If someone could help that would be really helpful! I am a young programmer and this is my first time asking on here.

Asked By: Yasser Machkour

||

Answers:

You are not using the character you replaced by "E" at all. You need to add it back to the list of characters. The simplest way to do that is to replace the item in the list at that index, and the index is provided by enumerate:

import random

old_level_map = [
'6--------------------------------------------------------------------7',
'L                                                                    I',
'9____________________________________________________________________8',
'                                                                      ',
'                                                                      ',
'      P                                                               ',
'                                                                      ',
'                                                                      ',
'6--------------------------------------------------------------------7',
'L                                                                    I',
'9____________________________________________________________________8']


level_map = []
for line in old_level_map:
    line = list(line)
    for n, char in enumerate(line):
        if char.isspace() and random.random() < 0.1:
            line[n] = "E"
    level_map.append("".join(line))
    
print(level_map)

['6--------------------------------------------------------------------7',
 'L         E E   EE              E                E         E   E     I',
 '9____________________________________________________________________8',
 'E                            E                   E  E   E E  E        ',
 '                            E  E                  E                   ',
 '      P  E     E     E E E   E E E             E         E         E  ',
 '       E                                E     E      EE   E          E',
 '     E        E            E     E                       E  E         ', 
 '6--------------------------------------------------------------------7', 
 'L          E E     E                 E             E                 I', 
 '9____________________________________________________________________8']

The same thing much shorter:

def randomly_replace(char: str) -> str:
    return "E" if char.isspace() and random.random() < 0.1 else char

level_map = ["".join(map(randomly_replace, line)) for line in old_level_map]
Answered By: Guimoute
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.