[Numpy-discussion] numpy.array does not take generators

Geoffrey Zhu zyzhu2000@gmail....
Fri Aug 17 17:26:55 CDT 2007


On 8/17/07, Robert Kern <robert.kern@gmail.com> wrote:
> Geoffrey Zhu wrote:
> > Hi All,
> >
> > I want to construct a numpy array based on Python objects. In the
> > below code, opts is a list of tuples.
> >
> > For example,
> >
> > opts=[ ('C', 100, 3, 'A'), ('K', 200, 5.4, 'B')]
> >
> > If I use a generator like the following:
> >
> > K=numpy.array(o[2]/1000.0 for o in opts)
> >
> > It does not work.
> >
> > I have to use:
> >
> > numpy.array([o[2]/1000.0 for o in opts])
> >
> > Is this behavior intended?
>
> Yes. With arbitrary generators, there is no good way to do the kind of
> mind-reading that numpy.array() usually does with sequences. It would have to
> unroll the whole generator anyways. fromiter() works for this, but you are
> restricted to 1-D arrays which is a lot easier to implement the mind-reading for.
>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless enigma
>  that is made terrible by our own mad attempt to interpret it as though it had
>  an underlying truth."
>  -- Umberto Eco
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>

I see. Thanks for explaining.


More information about the Numpy-discussion mailing list