疯狂的Python For Loop

时间:2014-07-22 19:01:40

标签: python

我正在写一个简单的跳棋程序。我试图得到一个很好的游戏状态的ASCII输出。这是我试图使用的功能:

 def function(self):
     board =  [["+", " - "]*8 + ["+", "\n"], \
               ["|", "   "]*8 + ["|", "\n"]]*8 + \
              [["+", " - "]*8 + ["+", "\n"]]

     i = 0
     for row in self.state:
         j = 0
         for cell in row:
             if cell == WHITE:
                 board[2*i+1][2*j+1] = ' w '
             elif cell == WHITE_KING:
                 board[2*i+1][2*j+1] = ' W '
             elif cell == BLACK:
                 board[2*i+1][2*j+1] = ' b '
             elif cell == BLACK_KING:
                 board[2*i+1][2*j+1] = ' B '
             j += 1
         i += 1

     return board

(在课程定义之外):

EMPTY, WHITE, WHITE_KING, BLACK, BLACK_KING = range(5)

我看不出它有什么问题,但现在正在发生的事情。

>>> B = CheckerBoard()
>>> B.state
[[3, 0, 3, 0, 3, 0, 3, 0], [0, 3, 0, 3, 0, 3, 0, 3], [3, 0, 3, 0, 3, 0, 3, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 1, 0, 1, 0, 1], [1, 0, 1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1, 0, 1]]
>>> print("".join(map(lambda x: "".join(x), B.function())))
+ - + - + - + - + - + - + - + - +
| w | w | w | w | w | w | w | w |
+ - + - + - + - + - + - + - + - +
| w | w | w | w | w | w | w | w |
+ - + - + - + - + - + - + - + - +
| w | w | w | w | w | w | w | w |
+ - + - + - + - + - + - + - + - +
| w | w | w | w | w | w | w | w |
+ - + - + - + - + - + - + - + - +
| w | w | w | w | w | w | w | w |
+ - + - + - + - + - + - + - + - +
| w | w | w | w | w | w | w | w |
+ - + - + - + - + - + - + - + - +
| w | w | w | w | w | w | w | w |
+ - + - + - + - + - + - + - + - +
| w | w | w | w | w | w | w | w |
+ - + - + - + - + - + - + - + - +

所需的输出是

+ - + - + - + - + - + - + - + - +
| b |   | b |   | b |   | b |   |
+ - + - + - + - + - + - + - + - +
|   | b |   | b |   | b |   | b |
+ - + - + - + - + - + - + - + - +
| b |   | b |   | b |   | b |   |
+ - + - + - + - + - + - + - + - +
|   |   |   |   |   |   |   |   |
+ - + - + - + - + - + - + - + - +
|   |   |   |   |   |   |   |   |
+ - + - + - + - + - + - + - + - +
| w |   | w |   | w |   | w |   |
+ - + - + - + - + - + - + - + - +
|   | w |   | w |   | w |   | w |
+ - + - + - + - + - + - + - + - +
| w |   | w |   | w |   | w |   |
+ - + - + - + - + - + - + - + - +

编辑:对于那些陷入类似地方的人来说,这是工作解决方案:

def function(self):
     board =  [x for list in
               [[[a for subl in [["+", " - "] for _ in range(8)] for a in subl] + ["+", "\n"], \
                [a for subl in [["|", "   "] for _ in range(8)] for a in subl] + ["|", "\n"]] \
                 for _ in range(8)] for x in list] + \
              [[a for subl in [["+", " - "] for _ in range(8)] for a in subl] + ["+", "\n"]]

     i = 0
     for row in self.state:
         j = 0
         for cell in row:
             if cell == WHITE:
                 board[2*i+1][2*j+1] = ' w '
             elif cell == WHITE_KING:
                 board[2*i+1][2*j+1] = ' W '
             elif cell == BLACK:
                 board[2*i+1][2*j+1] = ' b '
             elif cell == BLACK_KING:
                 board[2*i+1][2*j+1] = ' B '
             j += 1
         i += 1

     return board

1 个答案:

答案 0 :(得分:6)

一个常见的错误。 list*N执行浅拷贝,例如在x = …; l = [x]*4中,ll = [x,x,x,x]相同。现在,如果x是一个列表,那么您可以在多个位置编辑引用。

有关详细信息,请参阅官方Python常见问题解答中的How do I create a multidimensional list?。但简短版本是:代替[x]*4,写下[x for _ in range(4)],您将获得x的四个单独副本,而不是对同一x的四个引用。