[Numpy-discussion] alternative mechanism for initializing an array

Phillip M. Feldman pfeldman@verizon....
Thu Jul 16 20:34:33 CDT 2009


This does the right thing sometimes, but not always.  Out[2] and Out[4] 
are fine, but Out[3] is not (note the extra set of braces).  Probably 
the only right way to fix this is to modify numpy itself.

Phillip

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

In [2]: myarray([1,2,3],[4,5,6])
Out[2]:
array([[1, 2, 3],
       [4, 5, 6]])

In [3]: myarray([1,2,3])
Out[3]: array([[1, 2, 3]])

In [4]: np.array([1,2,3])
Out[4]: array([1, 2, 3])

From: Keith Goodman <kwgoodman <at> gmail.com>
Subject: Re: alternative mechanism for initializing an array 
<http://news.gmane.org/find-root.php?message_id=%3cf4f93d420907161357m99aefa3s83ae1ff043abb83e%40mail.gmail.com%3e>
Newsgroups: gmane.comp.python.numeric.general 
<http://news.gmane.org/gmane.comp.python.numeric.general>
Date: 2009-07-16 20:57:18 GMT (4 hours and 29 minutes ago)

On Thu, Jul 16, 2009 at 1:49 PM, David Warde-Farley<dwf <at> cs.toronto.edu> wrote:
> On 16-Jul-09, at 3:09 PM, Keith Goodman wrote:
>
>> def myarray(*args, **kwargs):
>>    return np.array([z for z in args], **kwargs)
>
>
> This version is better IMHO, because then you can still specify the
> dtype by keyword. although (just to be a pedant)
>
>> return np.array(args, **kwargs)
>
> works as well. :)

Oh, of course. Nice.



More information about the NumPy-Discussion mailing list