Skip to main content
 首页 » 编程设计

python之从 np.tril_indices 到 "reverse"索引的有效方法

2025年05月04日23cmt

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)) 

谢谢。

请您参考如下方法:

一种方式是

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