如何根据另一个带坐标的列表覆盖列表位置 - Python 3.6
How to overwrite list positions based on another list with coordinates - Python 3.6
我有一个列表(棋盘):
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()
这里是稍微修改过的数据格式。有了它,显示走法和显示整个网格变得更容易:
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
我有一个列表(棋盘):
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()
这里是稍微修改过的数据格式。有了它,显示走法和显示整个网格变得更容易:
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