我有一个ndarray子类,它实现了将一个或多个记录加载/保存到平面二进制文件中。在加载了这些记录之后,我可以按照正常的NumPy方式访问它们。
我的问题是关于当我对结果(或者任何NumPy数组)进行切片时会发生什么。这通常会产生一个“视图”,即一个引用与父数组相同缓冲区的数组。
一旦我有了这个视图,有没有办法确定视图V在数组A中的位置?更准确地说,我想知道从A的数据缓冲区开始算起,V开始的字节偏移量。这将使我能够在正确的偏移量上将切片写回磁盘。
以下是一些示例代码以展示这种情况:
我的问题是关于当我对结果(或者任何NumPy数组)进行切片时会发生什么。这通常会产生一个“视图”,即一个引用与父数组相同缓冲区的数组。
一旦我有了这个视图,有没有办法确定视图V在数组A中的位置?更准确地说,我想知道从A的数据缓冲区开始算起,V开始的字节偏移量。这将使我能够在正确的偏移量上将切片写回磁盘。
以下是一些示例代码以展示这种情况:
# Imagine a as consisting of 4 4-byte records...
a = np.arange(16, dtype='B').reshape(4,4)
# I select the first record
v = a[0]
print (v)
# [0 1 2 3]
# I can determine that v is a subarray:
is_subarray = v.base != None
# I can determine which dimension the slice spans..
whichdim = v.base.strides.index (v.strides[-1])
# But not its position along that dimension.