使用数组时如何减少代码中索引的使用?

How to reduce the use of indices in my code when working with arrays?

情况:

我一直在想很多方法来设置这个没有索引体操的板。我已经阅读了文档,差点淹死了。关于如何在不滥用索引的情况下使用数组的任何想法?我目前正在阅读 numpyitertools 和 Python 食谱中的一些食谱,但希望有任何想法。

# Set the x coordinates of Algebraic Chess Notation
files = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

# Name the squares on the board.
chessboard = [[x + str(y) for x in files] for y in range(1, 9, 1)]
 
# Here is the output
chessboard

[['a1', 'b1', 'c1', 'd1', 'e1', 'f1', 'g1', 'h1'],
 ['a2', 'b2', 'c2', 'd2', 'e2', 'f2', 'g2', 'h2'],
 ['a3', 'b3', 'c3', 'd3', 'e3', 'f3', 'g3', 'h3'],
 ['a4', 'b4', 'c4', 'd4', 'e4', 'f4', 'g4', 'h4'],
 ['a5', 'b5', 'c5', 'd5', 'e5', 'f5', 'g5', 'h5'],
 ['a6', 'b6', 'c6', 'd6', 'e6', 'f6', 'g6', 'h6'],
 ['a7', 'b7', 'c7', 'd7', 'e7', 'f7', 'g7', 'h7'],
 ['a8', 'b8', 'c8', 'd8', 'e8', 'f8', 'g8', 'h8']]

我想做什么:

我正在设置一个函数或方法来设置棋盘的初始状态作为示例。

我的想法:

def initial_state():
    """ Set the traditional initial positions of the pieces in the chessboard."""
    # White staff file
    for index in range(7):
        if index == 0 or index == 7:
            chessboard[0][index] = 'Rw_'+ chessboard[0][index]
        elif index == 1 or index == 6:
            chessboard[0][index] = 'Nw_'+ chessboard[0][index]
        elif index == 2 or index == 5:
            chessboard[0][index] = 'Bw_'+ chessboard[0][index]
        elif index == 3:
            chessboard[0][index] = 'Qw_'+ chessboard[0][index]
        elif index == 4:
            chessboard[0][index] = 'Kw_'+ chessboard[0][index]

    # Black staff file
    for index in range(7):
        if index == 0 or index == 7:
            chessboard[7][index] = 'Rb_'+ chessboard[7][index]
        elif index == 1 or index == 6:
            chessboard[7][index] = 'Nb_'+ chessboard[7][index]
        elif index == 2 or index == 5:
            chessboard[7][index] = 'Bb_'+ chessboard[7][index]
        elif index == 3:
            chessboard[7][index] = 'Qb_'+ chessboard[7][index]
        elif index == 4:
            chessboard[7][index] = 'Kb_'+ chessboard[7][index]

    # Pawn white file
    chessboard[1] = ['pw_' + x for x in chessboard[1]]

    # Pawn black file
    chessboard[6] = ['pb_' + x for x in chessboard[6]]

丑...我知道...

结果

initial_state()

# That is the result of the last input
[['Rw_a1', 'Nw_b1', 'Bw_c1', 'Qw_d1', 'Kw_e1', 'Bw_f1', 'Nw_g1', 'h1'],
 ['pw_a2', 'pw_b2', 'pw_c2', 'pw_d2', 'pw_e2', 'pw_f2', 'pw_g2', 'pw_h2'],
 ['a3', 'b3', 'c3', 'd3', 'e3', 'f3', 'g3', 'h3'],
 ['a4', 'b4', 'c4', 'd4', 'e4', 'f4', 'g4', 'h4'],
 ['a5', 'b5', 'c5', 'd5', 'e5', 'f5', 'g5', 'h5'],
 ['a6', 'b6', 'c6', 'd6', 'e6', 'f6', 'g6', 'h6'],
 ['pb_a7', 'pb_b7', 'pb_c7', 'pb_d7', 'pb_e7', 'pb_f7', 'pb_g7', 'pb_h7'],
 ['Rb_a8', 'Nb_b8', 'Bb_c8', 'Qb_d8', 'Kb_e8', 'Bb_f8', 'Nb_g8', 'h8']]

终于:

关于如何在不滥用索引的情况下使用一般数组的任何想法(在 python 3.4.2 中)?

我知道数组听起来像是棋盘的一个很好的表示,但为什么不使用字典呢? 您可以将位置作为键,将值作为片段:

chessboard = { "a1": "Rw", 
  "a2": "pw",
  "a3": "",
   ...
  "h7": "pb",
  "h8": ""}

您可以使用 chessboard['b2'] = "pw"

call/set/update 按键名的位置

这比数组和索引强大得多...您不一定需要填充空的 spaces 直到需要(但如果您应该检查是否存在 space我将通过 if 'b3' in chessboard.keys():

来调用它

享受学习Python...这是一门有趣而强大的语言!

对您的 BOLD 问题的一般回答:提取常见操作并利用 python 的迭代器函数,包括 zipenumerate。以下生成您的原始填充棋盘,行数更少。

files = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
chessboard = [[x + str(y) for x in files] for y in range(1, 9, 1)]
def board():
       print()
       for row in reversed(chessboard): print(row)
board()

for row, color in ((0, 'w'), (7, 'b')):
       for col, (label, piece) in enumerate(zip(chessboard[row],
              ('R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'))):
           chessboard[row][col] = piece + color + label
for row, color in ((1, 'w'), (6, 'b')):
       for col, label in enumerate(chessboard[row]):
           chessboard[row][col] = 'p' + color + label
board()

下面进一步进行因式分解。

for row, color in ((0, 'w'), (7, 'b'), (1, 'w'), (6, 'b')):
       for col, (label, piece) in enumerate(zip(chessboard[row],
              ('R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R')
              if row in (0, 7) else ('p',)*8)):
           chessboard[row][col] = piece + color + label