python - 你能判断一个数组是否是另一个数组的 View 吗?

标签 python numpy

numpy 数组是否跟踪它们的“ View 状态”?

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() 或其他东西。

我希望它用于代码分析,以确保我正确地做事并在我认为正确的时候获得意见。

最佳答案

数组还有一个base属性:

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

关于python - 你能判断一个数组是否是另一个数组的 View 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9164269/

相关文章:

Python numpy 减法没有负数(4-6 得到 254)

python 关闭后无法删除文件, "being used by another process"

Python 多线程

python - subprocess.Popen() 在 Eclipse/PyCharm 和终端执行之间有不一致的行为

python - 使用 numpy.vectorize() 旋转 NumPy 数组的所有元素

python - 单元测试 Django 时区感知日期时间

python - 将 bool 索引转换为运行的开始/结束对

python - numpy 的平均值大于 memmap 的最大值

python - 按一列分组并计算 Pandas 中的多个类别

python - 在 cython 中正确使用 numpy recarrays 作为 c structarrays