首页 > 解决方案 > 从 np.tril_indices “反转”索引的有效方法

问题描述

np.tril_indices(3)返回大小为 3 的方阵的下三角索引:

(array([0, 1, 1, 2, 2, 2], dtype=int64),
array([0, 0, 1, 0, 1, 2], dtype=int64))

什么是最有效的方法来代替:

(array([0, 1, 1, 2, 2, 2], dtype=int64),
array([0, 1, 0, 2, 1, 0], dtype=int64))

谢谢。

标签: pythonarraysnumpyindexing

解决方案


一种方法是

>>> np.subtract.accumulate(np.tril_indices(3), 0)
array([[0, 1, 1, 2, 2, 2],
       [0, 1, 0, 2, 1, 0]])

推荐阅读