I have a Numpy array and can successfully update all its elements with one line:
array_1 = np.array([1, 2, 3, 4])
array_1 = array_1 / 10.0
print(array_1)
# [0.1 0.2 0.3 0.4] -- Success!
However, when I have a list of Numpy arrays and iterate over them with a for in loop, I cannot apply the same operation and get back the desired results.
array_1 = np.array([1, 2, 3, 4])
array_2 = np.array([5, 6, 7, 8])
array_3 = np.array([9, 10, 11, 12])
print(array_1) # [1 2 3 4]
print(array_2) # [5 6 7 8]
print(array_3) # [ 9 10 11 12]
for array in [array_1, array_2, array_3]:
array = array / 10.0
print(array_1) # [1 2 3 4] -- No changes??
print(array_2) # [5 6 7 8]
print(array_3) # [ 9 10 11 12]
Why am I unable to update these arrays inside a loop? My understanding is that in the line
for array in [array_1, array_2, array_3]:
array will be a pointer to each of the three Numpy arrays.
How can I fix this problem?
array_1to the new value. In the second example, you are creating a new value, and changing the binding ofarray- butarray_1still contains the old value.arraymakes it point to the new array, it doesn't update the array that it points to.