Consider this simple problem: in a list of integers I need multiply all even number by 10. I can certainly do element-wise operation such as:
[if x%2==0: x=x*10 for x in arr]
But what if I the operation has to be operated on the array level? The trouble I am having is after the operation on the filtered array, how do I nicely put them back to the original array?
Code example:
arr=np.arange(1,10) # the original array array([1, 2, 3, 4, 5, 6, 7, 8, 9])
filter1 = arr%2==0 # the filter
arr1=arr[filter1] # the filtered array array([2, 4, 6, 8])
arr1=arr1*10 # the 'array'-wise operation array([20, 40, 60, 80])
# this is the part I am trying to improve
i=0
j=0
arr2=[]
for f in filter1:
if f:
arr2.append(arr1[i])
i=i+1
else:
arr2.append(arr[j])
j=j+1
# output arr2: [1, 20, 3, 40, 5, 60, 7, 80, 9]