Home > Back-end >  Lambda Functions in python 3.x
Lambda Functions in python 3.x

Time:08-27

How we can exclude the negative numbers from a given list and taking the square of only positive numbers being filtered. This all should be done using

a = map( ---- , array)

In the dash above, we have to write lambda function.

CodePudding user response:

You could filter positive numbers first and then apply the square:

map(lambda x: x**2, filter(lambda i: i>0, l))

Although there are more effective ways to do this (performance-wise) with a list comprehension:

[i**2 for i in l if i>0]

CodePudding user response:

Is this what you are looking for:

import numpy as np

array = np.arange(-5, 5, 1)
map(lambda x: x**2 if x > 0 else None, array)
  • Related