def func(x,y):
return(x+y)
func(2,3)
add=lambda x,y:x+y
add(2,3)
lambda functions can be used in place of a iterator when sorting -- this allows for selecting the column or the varible according to which sorting has to be done
import numpy as np
np.random.seed(42)
a1=np.random.choice(10,5,replace=False)
file_list=[]
for i in a1:
file_list.append('{0}-{1}'.format('Filename',i))
print(file_list)
file_list=sorted(file_list, key=lambda x:x.split('-')[-1])
file_list
def square(x):
return(x**2)
print(a1)
ans=[square(i) for i in a1]
print(ans)
map(square,a1)
list(map(square,a1))
a=np.random.choice(10,5,replace=False)
b=np.random.choice(50,5,replace=False)
result=map(lambda x,y:x*y,a,b)
print(a)
print(b)
print(np.asarray(list(result)))