0
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()

Question> I try to find the number that is dividable by 2 but 5. The Python compiler complains about the ambiguous. How do I fix the issue?

Thank you

q0987
  • 34,938
  • 69
  • 242
  • 387
  • Use `div_by2_notby5 = lambda x: (((x % 2) == 0) & ((x % 5) == 1))`. – jdehesa Apr 25 '19 at 13:34
  • I think the ambiguity is on the and condition not the callable returned by the lambda; if you replace and with `&` no Value error is returned, it actually works: `div_by2_notby5 = lambda x: ((x % 2) == 0) & ((x % 5) == 1)` prints `array([False, False, False])`. Trying to find out why or if that's truly the issue before i write a proper answer – nickyfot Apr 25 '19 at 13:36
  • 1
    actually, full explanation is [here](https://stackoverflow.com/questions/22646463/difference-between-and-boolean-vs-bitwise-in-python-why-difference-i) – nickyfot Apr 25 '19 at 13:38

0 Answers0