[Numpy-discussion] Constructing array from generator expression/iterator
oliphant at ee.byu.edu
Wed Feb 8 14:59:03 CST 2006
Stephan Tolksdorf wrote:
> I'm new to Numpy and just stumbled over the following problem in Numpy
> array(x**2 for x in range(10))
> does not return what one (me) would suspect, i.e.
> array([x**2 for x in range(10)])
> Is this expected behavior?
The array constructor does not current "understand" generators objects.
It only understands sequence objects. It could be made to work but is
based on code written long before there were generators.
So, instead you get a 0-d Object-array containing the generator.
Just use list comprehensions instead.
More information about the Numpy-discussion