How to apply any function over an array in python?

ipython

#1

I have created a function of subtraction of two number in Ipython notebook in the function I am giving the scalar input and getting scalar output .I want to know how to use the same function over an array.


#2

@sid100158,

Numpy provides np.vectorize helps Python functions to work on numpy arrays.

import numpy as np
vecfunc = np.vectorize(subtract) #Convert python function to vector function
result=vecfunc([3,2,1],[4,8,2]) #Call vector function to work on numpy arrays
print result

Thx,
Manuel