通过 2D 数组掩码 3D 数组以进行切片而无需 for 循环

Mask 3D-array by 2D-array for slicing without for loop

我有这样的东西

import numpy as np 

array_3D =  np.random.rand(3,3,3) 
array_2D = np.random.randint(0, 3 , (3,3)) 

for i in range(3):
    for j in range(3): 
        array_3D[:, i, j][:array_2D[i, j]]=np.nan  

有没有不用双 for 循环的方法?

用外ranged-comparison创建掩码然后赋值-

mask = np.less.outer(np.arange(len(array_3D)), array_2D)  
array_3D[mask] = np.nan