[Numpy-discussion] Is this odd?

Nadav Horesh nadavh at visionsense.com
Fri Apr 2 08:29:18 EDT 2010


In python empty sequences are always equivalent to False, and non-empty to True. You can use this property or:

if len(b) > 0:
   .....

  Nadav 


-----Original Message-----
From: numpy-discussion-bounces at scipy.org on behalf of Shailendra
Sent: Fri 02-Apr-10 06:07
To: numpy-discussion at scipy.org
Subject: [Numpy-discussion] Is this odd?
 
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).

Thanks,
Shailendra
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion at scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

-------------- next part --------------
A non-text attachment was scrubbed...
Name: winmail.dat
Type: application/ms-tnef
Size: 3104 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20100402/46dbf512/attachment.bin>


More information about the NumPy-Discussion mailing list