[Numpy-discussion] alternative mechanism for initializing an array

Keith Goodman kwgoodman@gmail....
Thu Jul 16 14:09:03 CDT 2009


On Thu, Jul 16, 2009 at 11:43 AM, Phillip M.
Feldman<pfeldman@verizon.net> wrote:
> numpy.array understands
>
> V= array([[1,2,3,4],[4,3,2,1]])
>
> but not
>
> V= array([1,2,3,4],[4,3,2,1])
>
> It would be more convenient if it could handle either form.

You could do something like this:

def myarray(*args):
    return np.array([z for z in args])

or

def myarray(*args, **kwargs):
    return np.array([z for z in args], **kwargs)

Then

>> myarray([1,2,3], [4,5,6])

array([[1, 2, 3],
       [4, 5, 6]])

>> myarray([1,2,3], [4,5,6], dtype=np.float64)

array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])


More information about the NumPy-Discussion mailing list