x = np.arange(9).reshape((3,3))
x
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
np.diag(x)
array([0, 4, 8])
np.diag(x, k=1)
array([1, 5])
np.diag(x, k=-1)
array([3, 7])
np.diag(np.diag(x))
array([[0, 0, 0],
[0, 4, 0],
[0, 0, 8]])
来源:CSDN
作者:侬开心就好
链接:https://blog.csdn.net/weixin_40717742/article/details/104441471