# [SciPy-User] [SciPy-user] ValueError: The truth value of an array with more than one element is ambiguous.

surfcast23 surfcast23@gmail....
Fri Apr 6 21:16:07 CDT 2012

```Thank you Tony

Tony Yu-3 wrote:
>
> On Fri, Apr 6, 2012 at 4:52 PM, surfcast23 <surfcast23@gmail.com> wrote:
>
>>
>> Hi Tony,
>>
>>  Thanks for the help. Would I be able to use the np.any and np.all
>> functions to count the number of true occurrences?
>>
>>
> Nope, but there are a few other ways of counting. The easiest is to call
> `np.sum` since True = 1, False = 0; e.g.:
>
>>>> np.sum(np.array([True, False, True, False, True]))
> 3
>
> You can also use `np.where` or `np.nonzero` to return indices of nonzero
> (i.e. True) elements and then get the length of the index array. Note,
> however, that these functions return a tuple of indices (with a length
> equal to the array dimensions) so you'll have to grab one of the index
> arrays first:
>
>>>> idx_true = np.nonzero(np.array([True, False, True, False, True]))
>>>> len(idx_true[0])
> 3
>
> Best,
> -Tony
>
>
>> Tony Yu-3 wrote:
>> >
>> > On Fri, Apr 6, 2012 at 12:54 AM, Tony Yu <tsyu80@gmail.com> wrote:
>> >
>> >>
>> >>
>> >> On Thu, Apr 5, 2012 at 6:46 PM, surfcast23 <surfcast23@gmail.com>
>> wrote:
>> >>
>> >>> Hi, I have an if statement and what I want it to do is go through
>> arrays
>> >>> and find the common elements in all three arrays. When I try the code
>> >>> below
>> >>> I get this error * ValueError: The truth value of an array with more
>> >>> than one element is ambiguous. Use a.any() or a.all()* Can some one
>> >>> explain the error to me and how I might be able to fix it. Thanks in
>> >>> advance. *if min <= Xa <=max & min <= Ya <=max & min <= Za <=max:
>> >>> print("in range") else: print("Not in range")*
>> >>
>> >>
>> >> This explanation may or may not be clear, but your question is
>> >> in this
>> >> communication<
>> http://mail.python.org/pipermail/python-ideas/2011-October/012278.html>
>> >> .
>> >>
>> >> Roughly:
>> >> 1) Python's default behavior for chained comparisons don't work as
>> you'd
>> >> expect for numpy arrays.
>> >> 2) Python doesn't allow numpy to change this default behavior (at
>> least
>> >> currently, and maybe
>> >> never<
>> http://mail.python.org/pipermail/python-dev/2012-March/117510.html>
>> >> ).
>> >>
>> >> Nevertheless, you can get around this by separating the comparisons
>> >>
>> >> >>> if (min <= Xa) & (Xa <= max):
>> >>
>> >> Note the use of `&` instead of `and`, which is at the heart of the
>> >> issue<http://www.python.org/dev/peps/pep-0335/>
>> >> .
>> >>
>> >> Hope that helps,
>> >> -Tony
>> >>
>> >
>> > Oops, I think I got myself mixed up in the explanation. Separating the
>> > comparisons fixes one error; For example, the following:
>> >
>> >>>> (min <= Xa) & (Xa <= max)
>> >
>> > will return an array of bools instead of raising an error (as you would
>> > get
>> > with `min <= Xa <= max`). This is what I meant to explain above.
>> >
>> > But, throwing an `if` in front of that comparison still doesn't work
>> > because it's ambiguous: Should `np.array([True False])` be true or
>> false?
>> > Instead you should check `np.all(np.array([True False]))`, which
>> evaluates
>> > as False since not-all elements are True, or `np.any(np.array([True
>> > False]))`, which evaluates as True since one element is True.
>> >
>> > -Tony
>> >
>> > _______________________________________________
>> > SciPy-User mailing list
>> > SciPy-User@scipy.org
>> > http://mail.scipy.org/mailman/listinfo/scipy-user
>> >
>> >
>>
>> --
>> View this message in context:
>> http://old.nabble.com/ValueError%3A-The-truth-value-of-an-array-with-more-than-one-element-is-ambiguous.-tp33583156p33645519.html
>> Sent from the Scipy-User mailing list archive at Nabble.com.
>>
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>

--
View this message in context: http://old.nabble.com/ValueError%3A-The-truth-value-of-an-array-with-more-than-one-element-is-ambiguous.-tp33583156p33647200.html
Sent from the Scipy-User mailing list archive at Nabble.com.

```