2019-04-12
阅读量:
537
如何使用numpy打印nxn的棋盘格式(2)
np.zeros((n,n),dtype = int)的功能:通常,数组的元素最初是未知的,但其大小是已知的。因此,NumPy提供了几个函数来创建具有初始占位符内容的数组。这些最小化了增加阵列的必要性,这是一项昂贵的操作使用dtype参数使用int data-type初始化所有值。
例如:np.zeros,np.ones等。
# 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条评论