[Numpy-discussion] array comprehension

Robert Kern robert.kern at gmail.com
Fri Nov 4 11:59:23 EDT 2016


On Fri, Nov 4, 2016 at 6:36 AM, Neal Becker <ndbecker2 at gmail.com> wrote:
>
> Francesc Alted wrote:
>
> > 2016-11-04 13:06 GMT+01:00 Neal Becker <ndbecker2 at gmail.com>:
> >
> >> I find I often write:
> >> np.array ([some list comprehension])
> >>
> >> mainly because list comprehensions are just so sweet.
> >>
> >> But I imagine this isn't particularly efficient.
> >>
> >
> > Right.  Using a generator and np.fromiter() will avoid the creation of
the
> > intermediate list.  Something like:
> >
> > np.fromiter((i for i in range(x)))  # use xrange for Python 2
> >
> >
> Does this generalize to >1 dimensions?

No.

--
Robert Kern
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20161104/b524c92e/attachment.html>


More information about the NumPy-Discussion mailing list