[Numpy-discussion] numpy.ndarray constructor from python list: bug?

Robin robince at gmail.com
Thu Mar 6 08:21:06 EST 2008


On Thu, Mar 6, 2008 at 9:53 AM, Emanuele Olivetti
<emanuele at relativita.com> wrote:
> Dear all,
>
>  Look at this little example:
>  ----
>  import numpy
>  a = numpy.array([1])
>  b = numpy.array([1,2,a])
>  c = numpy.array([a,1,2])
>  ----
>  Which has the following output:
>  ----
>  Traceback (most recent call last):
>   File "b.py", line 4, in <module>
>     c = numpy.array([a,1,2])
>  ValueError: setting an array element with a sequence.
>  ----
>
>  It seems that a list starting with an ndarray ('a', of
>  a single number) is not a legal input to build an ndarray.
>  Instead if 'a' is in other places of the list the ndarray
>  builds up flawlessly.
>
>  Is there a meaning for this behavior or is it a bug?
>
>  Details: numpy 1.04 on ubuntu linux x86_64

Hi,

I see the same behaviour with 1.0.5.dev4786. I think the bug is that
the b assignment should also fail. They both fail (as I think they
should) if you take a as an array with more than one element.

I think the array constructor expects lists of numbers, not of arrays
etc. To do what you want try

b = r_[1,2,a]
c = r_[a,1,2]
which works for a an array (and of more than one element).

Cheers

Robin



More information about the NumPy-Discussion mailing list