。。。但是更改numpy数组的值是可行的:
import numpy as np
def reshapeArray(arr):
arr = arr.reshape((2, 2))
arr /= 10
print(arr) # prints [[0.1 0.3], [0.2 0.4]]
arr = np.array([1, 2, 3, 4], dtype=np.float32)
reshapeArray(arr)
print(arr) # prints [0.1 0.2 0.3 0.4]
reshapeArray()
函数永久更改数组的值,但临时更改数组的形状。如果我在函数的末尾添加一个返回行(return arr
),并将函数的输出赋给数组(arr = reshapeArray(arr))
,那么这次就可以了。但是我想知道为什么它不返回数组就不能工作?你知道吗
尝试从函数返回数组,并将返回值赋给所需变量:
从文档(numpy.reshape):
与
arr = arr / 10
相反,它确实创建一个拷贝并重新分配它。你知道吗显然,离开范围时会丢失一个视图。。。你知道吗
相关问题 更多 >
编程相关推荐