To make it clearer you're using Numpy, I'll use an explicit import instead if from numpy import *
:
import numpy as np
if np.all(x>0 for x in np.array([-10,1,2])):
print np.sum(x>0 for x in np.array([-10,1,2]))
Here, x>0 for x in np.array([-10,1,2])
is a generator, and numpy.all
doesn't work on generators.
In addition, you might be interested in filtering arrays, to achieve what you want, using > 0
on the array directly as follows:
>>> import numpy as np
>>> a1 = np.array([-10,1,2])
>>> a2 = np.array([1,2,3])
>>> a1 > 0
array([False, True, True], dtype=bool)
>>> a2 > 0
array([ True, True, True], dtype=bool)
>>> np.all(a1 > 0)
False
>>> np.all(a2 > 0)
True
You may be interested in this: Boolean or “mask” index arrays
You'll find more about the behaviour of numpy.all
and generators in this answer.
Regarding your secondary question (why 2 instead of 3), it's because np.sum
treats True
as 1 and False
as 0, once you've passed the test condition. It's more visible if you expand the generator as follows:
>>> import numpy as np
>>> g = (x>0 for x in array([-10,1,2]))
>>> g
<generator object <genexpr> at 0x7f0edfc3b640>
>>> l = list(g)
>>> l
[False, True, True]
>>> np.sum(l)
2