你可以创建它,首先创建一个带零的矩阵,然后填充那些:import numpy as np# create matrix with zerosn=4mat = np.zeros((n,n))# create indexes for where the 1s belongrows = np.arange(1,n)cols = np.arange(n-1)# fill in the 1smat[rows, cols] = 1输出:[[0. 0. 0. 0.] [1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.]]