[Numpy-discussion] Create 2D array from EXISTING 1D array

Charles R Harris charlesr.harris@gmail....
Mon Sep 14 08:31:32 CDT 2009


On Mon, Sep 14, 2009 at 4:46 AM, Ruben Salvador <rsalvador.wk@gmail.com>wrote:

> Hi there!
>
> It's a time since I'm asking this question to myself, and still don't know
> a Pythonic way to solve it. I want to create a 2D array where each *row* is
> a copy of an already existing 1D array. For example:
>
> In [21]: a = np.array[1, 2, 3]
> In [25]: a
> Out[25]: array([1, 2, 3])
>
> To create a 2D 'b' array, where each row is 'a', I would do:
>
> In [28]: b = np.empty((5,3), np.int)
>
>
You can use a list of 1D arrays, i.e.,

In [1]: a = array([1,2,3])

In [2]: b = array([a]*5)

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

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090914/347587a9/attachment.html 


More information about the NumPy-Discussion mailing list