Home > Mobile >  Replace number with symbol in a list of lists
Replace number with symbol in a list of lists

Time:12-01

I need to replace 0 in a list of lists with dot ".". I aslo need to replace 1 with "o" and 2 with "*" It should be something like chess board. So far I have this and I am stuck with the replacement. Thank you for your help! :)

`

chess =[
    ["0 1 0 1 0 1 0 1 "],
    ["1 0 1 0 1 0 1 0 "],
    ["0 1 0 1 0 1 0 1 "],
    ["0 0 0 0 0 0 0 0 "],
    ["0 0 0 0 0 0 0 0 "],
    ["2 0 2 0 2 0 2 0 "],
    ["0 2 0 2 0 2 0 2 "],
    ["2 0 2 0 2 0 2 0 "]]


def prt (n):
   for i in range(len(n)):
            for j in range(len(n[i])):
                if n[j] == "0":
                    n[j]="."
                print(n[i][j])
       
prt(chess)

`

Output should be something like this

Final product

CodePudding user response:

I guess if you just need to print the layout, this would be a way to solve it:

chess = [
    ["0 1 0 1 0 1 0 1"],
    ["1 0 1 0 1 0 1 0"],
    ["0 1 0 1 0 1 0 1"],
    ["0 0 0 0 0 0 0 0"],
    ["0 0 0 0 0 0 0 0"],
    ["2 0 2 0 2 0 2 0"],
    ["0 2 0 2 0 2 0 2"],
    ["2 0 2 0 2 0 2 0"]
]

def prt(chess):
    for lst in chess:
        print(lst[0].replace("0", ".").replace("1", "o").replace("2", "*"))

prt(chess)

Result:

. o . o . o . o
o . o . o . o .
. o . o . o . o
. . . . . . . .
. . . . . . . .
* . * . * . * .
. * . * . * . *
* . * . * . * .

CodePudding user response:

You should use the replace function for strings.

chess =[
    ["0 1 0 1 0 1 0 1 "],
    ["1 0 1 0 1 0 1 0 "],
    ["0 1 0 1 0 1 0 1 "],
    ["0 0 0 0 0 0 0 0 "],
    ["0 0 0 0 0 0 0 0 "],
    ["2 0 2 0 2 0 2 0 "],
    ["0 2 0 2 0 2 0 2 "],
    ["2 0 2 0 2 0 2 0 "]]


def prt (n):
    new_list = []
    for line in n:
        line = line[0]
        line = line.replace('0','.')
        line = line.replace('1', 'o')
        line = line.replace('2', '*')
        temp = []
        temp.append(line)
        new_list.append(temp)
    print(new_list)
       
prt(chess)

CodePudding user response:

You can try it by using the replace function for strings and iterating through the lines itself:

def print_chess(chess):
    for line_list in chess:
        line = line_list[0]
        line = line.replace("0", ".")
        line = line.replace("1", "o")
        line = line.replace("2", "*")
        print(line)

Edit: thanks to @Muhammad Rizwan for pointing out that replace returns the result.

CodePudding user response:

In each list of your list you store one string, i.e. "0 1 0 1 0 1 0 1 ". So your loop doesn't work:

for i in range(len(n)): 
    for j in range(len(n[i])):  # len(n[i]) is always 1
        if n[j] == "0":  # n[j]: "0 1 0 1 0 1 0 1 " equal "0" is False
            n[j]="."

A comparison for an individual number won't work. In order for you to get the individual number you can use the split method on the only element of your list. So you need to access it via the 0 index and call split on that element. Build a new list of list for storing the modified values.

Using this, you can expand your if logic to check for "1" and "2" and for any other value to not change the value.

chess = [
    ["0 1 0 1 0 1 0 1 "],
    ["1 0 1 0 1 0 1 0 "],
    ["0 1 0 1 0 1 0 1 "],
    ["0 0 0 0 0 0 0 0 "],
    ["0 0 0 0 0 0 0 0 "],
    ["2 0 2 0 2 0 2 0 "],
    ["0 2 0 2 0 2 0 2 "],
    ["2 0 2 0 2 0 2 0 "]]

def prt(n):                                                                    
    res = []  # initialize empty list for chess board
    for line in n:
        numbers = line[0].split()  # splits at whitespace
        line_list = []  # empty list for each line
        for num in numbers:  # get individual number entry
            # Do the comparison and append modified value to line_list
            if num == "0":
                line_list.append(".")
            elif num == "1":
                line_list.append("o")
            elif num == "2":
                line_list.append("*")
            else:
                line_list.append(num)
        # Add line_list to res list to build a modified chess board
        res.append(line_list)

    # Print new chess board
    for line in res:
        for num in line:
            print(num, end=" ")
        print()
  • Related