F L F L - 2 years ago 86
Python Question

Replace value in array with condition

I have this simplified program to replace values in array which fulfill the conditions:


array = np.random.rand(2,4,10)
for n,i in enumerate(array):
if i>0.5: #find value in array with this condition
formula = formula1
array[n] = array[n]*formula #replace the found value with this value
elif i <0.1:
formula = formula2
array[n] = array[n]*formula
formula = formula3
array[n] = array[n]*formula
print array

It resulted in error message:'The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()'. Any suggestion?

Answer Source


for n,i in np.ndenumerate(array):
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download