python 将列表列表中的数字替换为符号

ffscu2ro  于 2022-12-02  发布在  Python
关注(0)|答案(5)|浏览(196)

我需要用点“."替换列表中的0。我还需要用“o”替换1,用“*”替换2。它应该是像棋盘一样的东西。到目前为止,我有这个,我坚持替换。谢谢你的帮助!:)

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)

输出应该如下所示

c8ib6hqw

c8ib6hqw1#

我想如果你只需要打印布局,这将是一个解决它的方法:

from string import ascii_lowercase

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):
    joined_chars = "".join(chess[0][0].split()) # joining the first list in chess to single string: 01010101
    letters = " ".join([ascii_lowercase[i] for i in range(len(joined_chars))]) # Creating a list of letters that is the length of the joined_chars and joins it with spaces: a b c d e f g h
    print(f"  {letters}") # prints the letters starting with two spaces
    for index, lst in enumerate(chess): # Using enumerate to get the index while running through chess
        printable = lst[0].replace("0", ".").replace("1", "o").replace("2", "*")
        print(f"{index+1} {printable} {index+1}") # prints the index (starts at 0) + 1 to get the correct values.
    print(f"  {letters}") # prints the letters starting with two spaces

prt(chess)

结果:

a b c d e f g h
1 . o . o . o . o 1
2 o . o . o . o . 2
3 . o . o . o . o 3
4 . . . . . . . . 4
5 . . . . . . . . 5
6 * . * . * . * . 6
7 . * . * . * . * 7
8 * . * . * . * . 8
  a b c d e f g h
whitzsjs

whitzsjs2#

对于字符串,应使用replace函数。

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)
new9mtju

new9mtju3#

在列表的每个列表中存储一个字符串,即"0 1 0 1 0 1 0 1 ",因此循环不起作用:

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]="."

对单个数字的比较是不起作用的。为了得到单个数字,你可以对列表中唯一的元素使用split方法。所以你需要通过0索引访问它,并对该元素调用split。建立一个新的list列表来存储修改后的值。
使用此方法,您可以扩展if逻辑,以检查"1""2"以及任何其他值,从而不更改该值。

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):
    letters = " abcdefgh"
    # Initialize chessboard with letters
    board = [letters]
    # set enumerates start value to 1
    for i, line in enumerate(n, 1):
        numbers = line[0].split()  # splits at whitespace
        line_list = []
        for num in numbers:
            if num == "0":
                line_list.append(".")
            elif num == "1":
                line_list.append("o")
            elif num == "2":
                line_list.append("*")
            else:
                line_list.append(num)
        # Concatenate current line index at beginning and end of line_list
        board.append([i] + line_list + [i])
    # Append letters as last line of board
    board.append(letters)
    # Print new chess board
    for line in board:
        for el in line:
            print(el, end=" ")
        print()

prt(chess)

变更内容:

  • letters设置为board的第一个和最后一个元素
  • 利用内置函数enumerate
  • enumeratestart值设置为1以获取修改后的索引

这将为您提供所需的结果:

a b c d e f g h 
1 . o . o . o . o 1 
2 o . o . o . o . 2 
3 . o . o . o . o 3 
4 . . . . . . . . 4 
5 . . . . . . . . 5 
6 * . * . * . * . 6 
7 . * . * . * . * 7 
8 * . * . * . * . 8 
  a b c d e f g h
ctrmrzij

ctrmrzij4#

你可以尝试用replace函数来替换字符串,并遍历行本身:

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)

编辑:感谢@Muhammad Rizwan指出replace返回结果。

fnx2tebb

fnx2tebb5#

非常感谢你的帮助。到目前为止我已经做到了这一点,但我不知道如何像图片中那样在行的开头和结尾放上数字。

我的新代码:

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 "]]

letters =["a b c d e f g h"]
numbers =["1 2 3 4 5 6 7 8"]

def prt(x):
    num_let(letters) 
    for list in x:
        new = list[0]
        new = new.replace("0", ".")
        new = new.replace("1", "o")
        new = new.replace("2", "*")
        print(new)
    num_let(letters)
   
def num_let (y):
    print(y[0])

prt(chess)

这就给了我这个:

a b c d e f g h
. o . o . o . o 
o . o . o . o .
. o . o . o . o
. . . . . . . .
. . . . . . . .
* . * . * . * .
. * . * . * . *
* . * . * . * .
a b c d e f g h

相关问题