Python 如何使用嵌套循环制作棋盘图案?

Python 如何使用嵌套循环制作棋盘图案?,python,loops,for-loop,nested,Python,Loops,For Loop,Nested,我需要做一个像这样的图案: 10101010 01101101101 10101010 01101101101 10101010 01101101101 10101010 01101101101 到目前为止,我只能用一个来画对角线,但我需要一些帮助来理解如何做到这一点 到目前为止,我的代码是: dimension = int(input('Enter dimension of board: ')) if dimension < 2: print('Invalid inpu

我需要做一个像这样的图案:

10101010

01101101101

10101010

01101101101

10101010

01101101101

10101010

01101101101

到目前为止,我只能用一个来画对角线,但我需要一些帮助来理解如何做到这一点

到目前为止,我的代码是:

dimension = int(input('Enter dimension of board: '))

if dimension < 2:    
    print('Invalid input')    
else:
    for r in range(dimension):
        for c in range(dimension):
            print(int(c == r), end=' ')
        print()
dimension=int(输入('输入电路板的尺寸:'))
如果尺寸小于2:
打印('无效输入')
其他:
对于范围内的r(尺寸):
对于范围内的c(尺寸):
打印(int(c==r),end='')
打印()

(c+r+1)%2
应该可以工作,而不是
int(c==r)
这将为您提供所需的电路板输出:

def printBoard(dimensions):
    for row in range(dimensions**2):
        if row % dimensions == 0 and row != 0:
            print()
            print((row + 1) % 2, end=' ')
        else:
            print((row + 1) % 2, end=' ')

这是巴拉比技术的一个变种。它使用嵌套的列表理解来构造偶数行和奇数行,然后使用
str.join
方法将每行的单元格连接成一个字符串。
make\u board
函数返回行字符串的列表,这些行字符串被合并成一个字符串,以便使用另一个
.join
调用进行打印

def make_board(side):
    r = range(side)
    rows = [' '.join(['01'[(i + p) % 2] for i in r]) for p in (1, 0)]
    return [rows[i % 2] for i in r]

# Test
for i in range(0, 9):
    board = make_board(i)
    print('{0}:\n{1}\n'.format(i, '\n'.join(board)))
输出

0:
1:
1.
2:
1 0
0 1
三:
1 0 1
0 1 0
1 0 1
4:
1 0 1 0
0 1 0 1
1 0 1 0
0 1 0 1
5:
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
6:
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
7:
1 0 1 0 1 0 1
0 1 0 1 0 1 0
1 0 1 0 1 0 1
0 1 0 1 0 1 0
1 0 1 0 1 0 1
0 1 0 1 0 1 0
1 0 1 0 1 0 1
8:
1 0 1 0 1 0 1 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
1 0 1 0 1 0 1 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

我知道,可能有点晚了,但这里有一个非常通用的同时也是快速的解决方案:

  1 import numpy as np
  2 import matplotlib.pyplot as plt
  3 import time
  4 from PIL import Image
  5 import os
  6
  7 def divmod_max_into(div_mod):
  8     if div_mod[1] > 0:
  9         return div_mod[0] + 1
 10     else:
 11         return div_mod[0]
 12 def create_chessboard(dim_x, dim_y, cube_size):
 13     start = time.time()
 14     light_grey = 230
 15     dark_grey = 90
 16     divmod_x_cube = divmod(dim_x, cube_size*2)
 17     divmod_y_cube = divmod(dim_y, cube_size*2)
 18     large_cube = np.full([cube_size*2, cube_size*2], False)
 19     large_cube[:cube_size, :cube_size] = True
 20     large_output = np.tile(large_cube, (divmod_max_into(divmod_x_cube), divmod_max_into(divmod_y_cube)))
 21     large_output = np.transpose(large_output) + large_output == 2
 23     output = np.full(large_output.shape, dark_grey, dtype=np.uint8)
 24     output[large_output] = 230
 25     print("Execution took {:.6f} seconds!".format(time.time()-start))
 26     return output[:dim_x, :dim_y]
 27
 28 Image.fromarray(create_chessboard(5000, 5000, 3)).save(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'out.tif'))
您还可以定义多维数据集的大小,这应该重复。在我的电脑上创建5000x5000阵列大约需要0.74秒


如果有什么不清楚的地方,尽管问。

这会让你达到你想要的目的

for r in range(dimension):
    for c in range(dimension):
        print(int((c + r + 1) % 2), end=' ')
    print()

维度
为偶数时,此操作无法正常工作:所有行都以“1”开头。尽管它使左上角为“0”而不是“1”,但此操作仍然有效。
for r in range(dimension):
    for c in range(dimension):
        print(int((c + r + 1) % 2), end=' ')
    print()