Thanks everyone for replies/suggestion. It is simple to avoid this
problem. But my point that given the behavior of python this behavior
seems inconsistent. There could other method provided which could
evaluate bool value depending on values stored in the array.

Thanks,
Shailendra

On Fri, Apr 2, 2010 at 10:28 AM, Ryan May <rma...@gmail.com> wrote:
> On Thu, Apr 1, 2010 at 10:07 PM, Shailendra <shailendra.vi...@gmail.com> 
> wrote:
>> Hi All,
>> Below is some array behaviour which i think is odd
>>>>> a=arange(10)
>>>>> a
>> array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>>>> b=nonzero(a<0)
>>>>> b
>> (array([], dtype=int32),)
>>>>> if not b[0]:
>> ...     print 'b[0] is false'
>> ...
>> b[0] is false
>>
>> Above case the b[0] is empty so it is fine it is considered false
>>
>>>>> b=nonzero(a<1)
>>>>> b
>> (array([0]),)
>>>>> if not b[0]:
>> ...     print 'b[0] is false'
>> ...
>> b[0] is false
>>
>> Above case b[0] is a non-empty array. Why should this be consider false.
>>
>>>>> b=nonzero(a>8)
>>>>> b
>> (array([9]),)
>>>>> if not b[0]:
>> ...     print 'b[0] is false'
>> ...
>>>>>
>> Above case b[0] is non-empty and should be consider true.Which it does.
>>
>> I don't understand why non-empty array should not be considered true
>> irrespective to what value they have.
>> Also, please suggest the best way to differentiate between an empty
>> array and non-empty array( irrespective to what is inside array).
>
> But by using:
>
> if not b[0]:
>
> You're not considering the array as a whole, you're looking at the
> first element, which is giving expected results.  As I'm sure you're
> aware, however, you can't simply do:
>
> if not b: # Raises exception
>
> So what you need to do is:
>
> if b.any():
>
> or:
>
> if b.all()
>
> Now for determining empty or not, you'll need to look at len(b) or b.shape
>
> Ryan
>
> --
> Ryan May
> Graduate Research Assistant
> School of Meteorology
> University of Oklahoma
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to