如何将Lambda与numpy数组一起使用

时间:2019-04-25 13:29:58

标签: python-3.x numpy

In [90]: qty = np.array([100, 200, 150])
In [91]: div_by_2 = lambda x: (x % 2) == 0
In [92]: div_by_2(qty)
Out[92]: array([ True,  True,  True])

In [93]: div_by2_notby5 = lambda x: (((x % 2) == 0) and ((x % 5) != 0))
In [94]: div_by2_notby5(10)
Out[94]: False
In [95]: div_by2_notby5(6)
Out[95]: True

In [97]: div_by2_notby5(qty)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-97-ee3b041f6045> in <module>()
----> 1 div_by2_notby5(qty)

<ipython-input-93-9b7250c23a2a> in <lambda>(x)
----> 1 div_by2_notby5 = lambda x: (((x % 2) == 0) and ((x % 5) == 1))

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

问题>我试图找到可被2除以5的数字。Python编译器抱怨模棱两可。我该如何解决该问题?

谢谢

0 个答案:

没有答案