[英]Lambda Functions in python 3.x
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.在上面的破折号中,我们必须写 lambda function。
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)
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.