Per the Python docs:
Comparisons can be chained arbitrarily, e.g.,?x?<?y?<=?z
?is equivalent to?x?<?y?and?y?<=?z
, except that?y
?is evaluated only once (but in both cases?z
?is not evaluated at all when?x?<?y
?is found to be false).
So your example is equivalent to:
1 < np.array([1, 2, 3]) and np.array([1, 2, 3]) < 3
so each subterm is expected to result in a boolean value. But the subterm:
1 < np.array([1, 2, 3])
results in a new numpy array containing:
[False, True, True]
It is this value that Python is trying to interpret as a boolean value. It fails to do that, producing the error message:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I expect that the desired expression here is:
(1 < np.array([1, 2, 3])).all() and (np.array([1, 2, 3]) < 3).all()
which can't be simplified to use comparison chaining.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…