你能判断一个数组是否是另一个数组的视图?

时间:2012-02-06 17:22:37

标签: python numpy

numpy数组是否跟踪他们的“查看状态”?

import numpy
a = numpy.arange(100)
b = a[0:10]
b[0] = 100
print a[0]
# 100 comes out as it is a view
b is a[0:10]
# False (hmm how to ask?)

我要找的是numpy.isview()或其他什么。

我希望这能用于代码分析,以确保我正确地做事并在我认为自己时获得意见。

2 个答案:

答案 0 :(得分:5)

该数组还有一个基本属性:

a = np.arange(10)
print a.base
None

b = a[2:9]
print b.base is a
True

c = b[:2]
print c.base is b
True
print c.base is a
False

答案 1 :(得分:3)

ndarray.flags.owndata告诉您阵列是否拥有其数据。在您的示例中:

In [18]: a.flags.owndata
Out[18]: True

In [19]: b.flags.owndata
Out[19]: False

它显然不如你所要求的那么精确,但它是我所知道的最好的。

相关问题