[Numpy-discussion] numpy.asarray( iterator )

Timothy Hochberg tim.hochberg at ieee.org
Mon Feb 4 13:28:49 EST 2008


On Mon, Feb 4, 2008 at 6:56 AM, Sebastian Haase <haase at msg.ucsf.edu> wrote:

> Hi,
>
> Can this be changed:
> If I have a list L the usual N.asarray( L )  works well -- however I
> just discovered that N.asarray( reversed( L ) )  breaks my code....
>
> Apparently reversed( L ) returns an iterator object, and N.asarray(
> reversed( L ) )   (called arrY in my function)
> results in:
> (Pdb) p arrY
> array(<listreverseiterator object at 0x1a508a90>, dtype=object)
> (Pdb) p arrY.shape
> ()
>
>
> Comments ?   How about letting asarray call fromiter  when it sees
> that the argument is a iterator !?


That's not really feasible.  fromiter requires knowledge of the type of the
data which you don't in general know in asarray. In addition, the various
array creations are already teetering on the edge of having too much magic
built in, and IMO it's a mistake to try to do any more guessing than we
already do.

My suggestion is to not use reversed. If your input (L) is a sequence rather
than an iterator, why not just use L[::-1]. If L, might be an iterator you
might need to do something else, but in any case, you will know more about
the possible types of L than asarray can, so you are more able to make some
sensible decisions about how to treat it.

If you don't care about efficiency, then I believe array(list(L)) should
work in a lot of cases, but it's pretty horribly inefficient.


>
> Thanks,
> Sebastian Haase
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>



-- 
.  __
.   |-\
.
.  tim.hochberg at ieee.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20080204/af8e6e9b/attachment.html>


More information about the NumPy-Discussion mailing list