'Inequality condition as argument of np.any() function [duplicate]

I'm trying to run code containing the following:

if np.any(1.5<=np.abs(Array1 - Array2)<=2):
     return True

However, when I run this, I get the error: Truth value of an array with multiple elements is ambiguous, use a.any() or a.all(). How do I implement this inequality condition in the np.any() function without error?



Solution 1:[1]

It seems like you can't quite do concise inequalities with numpy like you can in the rest of Python. Instead, just do two separate conditions and combine them:

tmp = np.abs(Array1 - Array2)
if np.any((1.5 <= tmp) & (tmp <= 2)):
     return True

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 richardec