Python 在二维数组中创建黑白棋盘
有没有更好(更短)的方法来创建类似棋盘的数组。对董事会的要求如下:Python 在二维数组中创建黑白棋盘,python,Python,有没有更好(更短)的方法来创建类似棋盘的数组。对董事会的要求如下: 电路板的尺寸可以不同(在我的示例中为3x3) 板的左下方正方形应始终为黑色 黑色方块由“B”表示,白色方块由“W” 我拥有的代码: def isEven(number): return number % 2 == 0 board = [["B" for x in range(3)] for x in range(3)] if isEven(len(board)): for rowIndex, row in
- 电路板的尺寸可以不同(在我的示例中为3x3)
- 板的左下方正方形应始终为黑色
- 黑色方块由
表示,白色方块由“B”
“W”
def isEven(number):
return number % 2 == 0
board = [["B" for x in range(3)] for x in range(3)]
if isEven(len(board)):
for rowIndex, row in enumerate(board):
if isEven(rowIndex + 1):
for squareIndex, square in enumerate(row):
if isEven(squareIndex + 1):
board[rowIndex][squareIndex] = "W"
else:
for squareIndex, square in enumerate(row):
if not isEven(squareIndex + 1):
board[rowIndex][squareIndex] = "W"
else:
for rowIndex, row in enumerate(board):
if not isEven(rowIndex + 1):
for squareIndex, square in enumerate(row):
if isEven(squareIndex + 1):
board[rowIndex][squareIndex] = "W"
else:
for squareIndex, square in enumerate(row):
if not isEven(squareIndex + 1):
board[rowIndex][squareIndex] = "W"
for row in board:
print row
['B', 'W', 'B']
['W', 'B', 'W']
['B', 'W', 'B']
输出:
def isEven(number):
return number % 2 == 0
board = [["B" for x in range(3)] for x in range(3)]
if isEven(len(board)):
for rowIndex, row in enumerate(board):
if isEven(rowIndex + 1):
for squareIndex, square in enumerate(row):
if isEven(squareIndex + 1):
board[rowIndex][squareIndex] = "W"
else:
for squareIndex, square in enumerate(row):
if not isEven(squareIndex + 1):
board[rowIndex][squareIndex] = "W"
else:
for rowIndex, row in enumerate(board):
if not isEven(rowIndex + 1):
for squareIndex, square in enumerate(row):
if isEven(squareIndex + 1):
board[rowIndex][squareIndex] = "W"
else:
for squareIndex, square in enumerate(row):
if not isEven(squareIndex + 1):
board[rowIndex][squareIndex] = "W"
for row in board:
print row
['B', 'W', 'B']
['W', 'B', 'W']
['B', 'W', 'B']
有点像黑客但是
print [["B" if abs(n - row) % 2 == 0 else "W" for n in xrange(3)] for row in xrange(3)][::-1]
这看起来像是需求爬行或是什么=)
def make_板(n):
''返回n的空列表如何:
>>> n = 3
>>> board = [["BW"[(i+j+n%2+1) % 2] for i in range(n)] for j in range(n)]
>>> print board
[['B', 'W', 'B'], ['W', 'B', 'W'], ['B', 'W', 'B']]
>>> n = 4
>>> board = [["BW"[(i+j+n%2+1) % 2] for i in range(n)] for j in range(n)]
>>> print board
[['W', 'B', 'W', 'B'], ['B', 'W', 'B', 'W'], ['W', 'B', 'W', 'B'], ['B', 'W', 'B', 'W']]
此选项正确地将左下角设置为“B”,始终:
def board(n):
def line(n, offset):
return [(i+offset) % 2 and 'W' or 'B' for i in range(n)]
return [line(n,i) for i in range(n+1,1,-1)]
以下是一个解决方案:
from itertools import cycle
N = 4
colors = cycle(["W","B"])
row_A = [colors.next() for _ in xrange(N)]
if not N%2: colors.next()
row_B = [colors.next() for _ in xrange(N)]
rows = cycle([row_A, row_B])
board = [rows.next() for _ in xrange(N)]
对于N=4
['W', 'B', 'W', 'B']
['B', 'W', 'B', 'W']
['W', 'B', 'W', 'B']
['B', 'W', 'B', 'W']
如果您确保将每一行和循环添加到行列表中,那么这应该可以扩展到多种颜色(比如一块显示为“B”、“W”、“G”的板)。非常容易理解。此外,还可以生成矩形板:
def gen_board(width, height):
bw = ['B', 'W']
l = [[bw[(j + i) % 2] for j in range(width)] for i in range(height)]
# this is done to make sure B is always bottom left
# alternatively, you could do the printing in reverse order
l.reverse()
## or, we could ensure B is always bottom left by adjusting the index
#offset = height%2 + 1
#l = [[bw[(j + i + offset) % 2] for j in range(width)] for i in range(height)]
return l
def print_board(b):
for row in b:
print row
试驾:
>>> print_board(gen_board(4, 3))
['B', 'W', 'B', 'W']
['W', 'B', 'W', 'B']
['B', 'W', 'B', 'W']
带一行numpy代码,不带for循环:
import numpy as np
chessbool = (np.arange(3)[:, None] + np.arange(3)) % 2 == 0
输出为:
array([[ True, False, True],
[False, True, False],
[ True, False, True]]
array([['B', 'W', 'B'],
['W', 'B', 'W'],
['B', 'W', 'B']])
用W
和B
填充数组:
chessboard = np.where(chessbool,'B','W')
输出为:
array([[ True, False, True],
[False, True, False],
[ True, False, True]]
array([['B', 'W', 'B'],
['W', 'B', 'W'],
['B', 'W', 'B']])
我相信你会有一些使用模数的情况。每个位置的颜色将基于行数和列数。您是否考虑过将其制作成一个平面列表,按顺序填写适当的值,然后将其重新组装到MxN网格中?IMO,首先使用位图--他们在所有方面都非常优秀:内存占用,计算速度。。。它的设计也比嵌套的字符串列表更优雅。左下角的应该是“B”@piokuc谢谢,修正了。我所要做的就是改变开始顺序。直到现在我才意识到棋盘是有方向的!对于n=4,它不能正常工作,因为左下角不是黑色,现在一切正常。把它变成一个函数会很好——它会更容易使用;)为什么要用指数做这些花哨的事情<代码>(i+j)%2
就足够了。@安卓:不,不是。如果您只使用(i+j)%2,您不会总是得到“电路板左下角的正方形应始终为黑色”,我也不会想到您的反转技巧。@DSM我现在看到了。我想我只是不喜欢它在理解中的样子,因为在建立列表之前可以计算一次n%2+1
。哪一行是一行?这一行chessbool=(np.arange(3)[:,None]+np.arange(3))%2==0
。其他行是填充W
和B
布尔数组,只有第一行输出是array([[True,False,True,False],[True,False],[True,False,True]])
注意,我想你可以做np.where(chessbool,'B','W')
。