首页 > 解决方案 > 当你重塑它时,numpy 数组的 getrefcount 发生了什么

问题描述

重塑 np.array 时,内存块中发生了什么?

一旦我做 sys.gerefcount。我得到 print(sys.getrefcount(foo2)) 和 print(sys.getrefcount(foo1)) 的不同结果


foo1 = np.array([1,2,3])
foo2 = foo1.reshape(3,1)

print(sys.getrefcount(foo1))
print(sys.getrefcount(foo2))

标签: python

解决方案


我假设:

print(sys.getrefcount(foo1))
2
print(sys.getrefcount(foo2))
3

根据文档:

sys.getrefcount(object)

    Return the reference count of the object. The count returned is generally one higher than you might expect, because it includes the (temporary) reference as an argument to getrefcount().

使用您的命令:foo2 = foo1.reshape(3,1)您引用对象 1,因此总共导致 3 个引用


推荐阅读