[Numpy-discussion] numpy all unexpected result (generator)

Dag Sverre Seljebotn d.s.seljebotn@astro.uio...
Tue Jan 31 09:19:29 CST 2012


On 01/31/2012 04:13 PM, Benjamin Root wrote:
>
>
> On Tuesday, January 31, 2012, Alan G Isaac <alan.isaac@gmail.com
> <mailto:alan.isaac@gmail.com>> wrote:
>  > On 1/31/2012 8:26 AM, Neal Becker wrote:
>  >> I was just bitten by this unexpected behavior:
>  >>
>  >> In [24]: all ([i>   0 for i in xrange (10)])
>  >> Out[24]: False
>  >>
>  >> In [25]: all (i>   0 for i in xrange (10))
>  >> Out[25]: True
>  >>
>  >> Turns out:
>  >> In [31]: all is numpy.all
>  >> Out[31]: True
>  >
>  >
>  >>>> np.array([i>  0 for i in xrange (10)])
>  > array([False,  True,  True,  True,  True,  True,  True,  True,  True,
>   True], dtype=bool)
>  >>>> np.array(i>  0 for i in xrange (10))
>  > array(<generator object <genexpr> at 0x0267A210>, dtype=object)
>  >>>> import this
>  >
>  >
>  > Cheers,
>  > Alan
>  >
>
> Is np.all() using np.array() or np.asanyarray()?  If the latter, I would
> expect it to return a numpy array from a generator.  If the former, why
> isn't it using asanyarray()?

Your expectation is probably wrong:

In [12]: np.asanyarray(i for i in range(10))
Out[12]: array(<generator object <genexpr> at 0x455d9b0>, dtype=object)

Dag Sverre


More information about the NumPy-Discussion mailing list