我有一个索引IDX
(可能是索引列表、布尔掩码、切片元组等)索引一些已知形状的抽象 numpy 数组shape
(可能很大)。
我知道我可以创建一个虚拟数组,对其进行索引并计算元素:
A = np.zeros(shape)
print(A[IDX].size)
有没有什么明智的方法可以在不创建任何(可能很大)数组的情况下获得索引元素的数量?
我需要将 3D 空间中某些点的函数列表制成表格。这些点是给定为X
、Y
、Z
列表的矩形网格的子集,并且IDX
正在索引它们的笛卡尔积:
XX, YY, ZZ = [A[IDX] for A in np.meshgrid(X, Y, Z)]
这些函数接受X
, Y
,Z
参数(并返回需要索引的笛卡尔积的值)或XX
, YY
, ZZ
。在我创建XX
,YY
和ZZ
数组的那一刻,无论它们是否被使用,然后我为函数值分配一个数组:
self.TAB = np.full((len(functions), XX.size),
np.nan)
但我想创建XX
,YY
并且ZZ
仅在必要时创建。我还想将TAB
分配与填充行分开,因此我需要提前知道列数。