[Numpy-discussion] numpy.asarray( iterator )
Mon Feb 4 12:28:49 CST 2008
On Mon, Feb 4, 2008 at 6:56 AM, Sebastian Haase <email@example.com> wrote:
> 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
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.
> Sebastian Haase
> Numpy-discussion mailing list
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Numpy-discussion