如何基于另一个带坐标的列表覆盖列表位置 - Python 3.6

时间:2017-11-10 10:13:40

标签: python python-3.6

我有一个列表(chessBoard):

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

1 个答案:

答案 0 :(得分:1)

这是一种略微修改的数据格式。有了它,显示移动和显示整个网格变得更容易:

let scrollcontent = document.getElementsByClassName('ion-page')[0].getElementsByTagName('ion-content')[0].getElementsByClassName('scroll-content')[0]
let rect = scrollcontent.getBoundingClientRect()
document.getElementById('refresher').style.top = rect.top.toString() + 'px'
this.myRefresher._beginRefresh()

输出:

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