热线电话:13121318867

登录
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

120.0000
2
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子