Helenr
您可以实现与diagonal仅使用arangefor 行和列相同的行为。在建立索引之前删除您不感兴趣的索引(正如@hpaulj 在评论中指出的那样,实际上只是找到对角线并在之后删除索引会更快):设置a = np.arange(25).reshape(5,5)i = 3 # exclude the diagonal element at index 3array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]])d = np.arange(a.shape[0])m = np.delete(d, i)a[m, m]array([ 0, 6, 12, 24])