gpt4 book ai didi

python - 如何根据另一个带有坐标的列表覆盖列表位置 - Python 3.6

转载 作者:太空宇宙 更新时间:2023-11-03 14:31:49 25 4
gpt4 key购买 nike

我有一个列表(棋盘):

chessBoard = [["_|"] * 8 for i in range(8)]

我有一个带有坐标的列表:

y = [(1, 2), (1, 4), (5, 2), (5, 4), (2, 1), (2, 5), (4, 1), (4, 5)]

例如,如果我们选择 (5,2) 那么我需要将列表 x 中第 5 列第 2 行的内容替换为 '*'。我有点迷失了如何做到这一点,也许我应该使用数组而不是 x 列表。就像我说的——我不知道。任何帮助,将不胜感激。谢谢。

chessBoard = [["|_"] * 8 for i in range(8)]
moves = [(1, 2), (1, 4), (5, 2), (5, 4), (2, 1), (2, 5), (4, 1), (4, 5)]

index_to_letter = {
0: "a",
1: "b",
2: "c",
3: "d",
4: "e",
5: "f",
6: "g",
7: "h"
}

def test():
x = 0
y = 8
for i in range(len(chessBoard)):
print(*chessBoard[i],end="")
if i%8==x:
print("",y)
x += 1
y -= 1
for i in range(8):
print("",index_to_letter[i],end=" ")

test()

最佳答案

这是稍微修改过的数据格式。有了它,显示 Action 和显示整个网格变得更加容易:

chessBoard = [["_"] * 8 for i in range(8)]
moves = [(1, 2),(1, 4),(5, 2),(5, 4),(2, 1),(2, 5),(4, 1),(4, 5)]

# Add a symbol on the grid for every move
for i, j in moves:
chessBoard[i][j] = "X"

index_to_letter = 'abcdefgh'

# Display board with row numbers
for i, row in enumerate(chessBoard):
print(' | '.join(row) + ' ' + index_to_letter[i])

它输出:

_ | _ | _ | _ | _ | _ | _ | _ a
_ | _ | X | _ | X | _ | _ | _ b
_ | X | _ | _ | _ | X | _ | _ c
_ | _ | _ | _ | _ | _ | _ | _ d
_ | X | _ | _ | _ | X | _ | _ e
_ | _ | X | _ | X | _ | _ | _ f
_ | _ | _ | _ | _ | _ | _ | _ g
_ | _ | _ | _ | _ | _ | _ | _ h

关于python - 如何根据另一个带有坐标的列表覆盖列表位置 - Python 3.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47220380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com