[Numpy-discussion] Creating an ndarray from an iterable over sequences

Dr. Leo fhaxbox66 at googlemail.com
Tue Jan 21 01:34:19 EST 2014


Hi,

I would like to write something like:

In [25]: iterable=((i, i**2) for i in range(10))

In [26]: a=np.fromiter(iterable, int32)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call
last)
<ipython-input-26-5bcc2e94dbca> in <module>()
----> 1 a=np.fromiter(iterable, int32)

ValueError: setting an array element with a sequence.


Is there an efficient way to do this?

Creating two 1-dimensional arrays first is costly as one has to
iterate twice over the data. So the only way I see is creating an
empty [10,2] array and filling it row by row. This is memory-efficient
but slow. List comprehension is vice versa.

If there is no solution, wouldn't it be possible to rewrite fromiter
so as to accept sequences?

Leo




More information about the NumPy-Discussion mailing list