[Numpy-discussion] converting scalar to array with dimension 1
Fri Mar 30 14:50:38 CDT 2007
Actually, there's even faster than that:
a = 3
a = array(a, ndmin=1)
atleast_1d is nothing but a wrapper function, that works best when used with
several inputs. When using only one array as inputs, the trick above should
be more appropriate.
On 3/30/07, Bill Baxter <email@example.com> wrote:
> atleast_1d will do the trick
> In : a = 3
> In : a = atleast_1d(a)
> In : shape(a)
> Out: (1,)
> In : a.shape # also works ;-)
> Out: (1,)
> In : a
> Out: 3
> On 3/30/07, Mark Bakker <firstname.lastname@example.org> wrote:
> > Hello list -
> > I have a function that normally accepts an array as input, but sometimes
> > scalar.
> > I figured the easiest way to make sure the input is an array, is to make
> > an array.
> > But if I make a float an array, it has 0 dimension, and I can still not
> > array manipulation on it.
> > >>> a = 3
> > >>> a = array(a)
> > >>> shape(a)
> > ()
> > >>> a
> > Traceback (most recent call last):
> > File "<pyshell#121>", line 1, in ?
> > a
> > IndexError: 0-d arrays can't be indexed
> > What would be the best (and easiest, this is for an intro class I am
> > teaching) way
> > to convert a to an array (recall, most of the time a is already an
> > Thanks for your help, Mark
> > _______________________________________________
> > Numpy-discussion mailing list
> > Numpyemail@example.com
> > http://projects.scipy.org/mailman/listinfo/numpy-discussion
> Numpy-discussion mailing list
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Numpy-discussion