2019-01-10
阅读量:
874
如何使用numpy打印nxn的棋盘格式(2)
# Python program to print nXn
# checkerboard pattern using numpy
import numpy as np
# function to print Checkerboard pattern
def printcheckboard(n):
print("Checkerboard pattern:")
# create a n * n matrix
x = np.zeros((n, n), dtype = int)
# fill with 1 the alternate rows and columns
x[1::2, ::2] = 1
x[::2, 1::2] = 1
# print the pattern
for i in range(n):
for j in range(n):
print(x[i][j], end =" ")
print()
# driver code
n = 8
printcheckboard(n)
输出:
棋盘图案:
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 0 1 0 1 0 1 0






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
0条评论