[Numpy-discussion] Creating small arrays from strings and concatenating with empty arrays

Bill Baxter wbaxter at gmail.com
Thu May 11 01:28:01 EDT 2006


Two quick questions:
---------1------------
Is there any better way to intialize an array from a string than this:

    A = asarray(matrix("1 2 3"))

Or is that as good as it gets?  I suppose it's not so inefficient.

----------2-------------

A lot of time I'd like to be able to write loops like this:

A = array([])
for row  in  function_that_returns_iterable_of_one_d_arrays():
     A = vstack((A,row))

But that generates an error the first iteration because the shape of A is
wrong.

I could stick in a reshape in the loop:
A = array([])
for row  in  function_that_returns_iterable_of_one_d_arrays():
     if not A.shape[0]: A.reshape(0,row.shape[0])
     A = vstack((A,row))

Meh, but I don't really like looking at 'if's in loops when I know they're
really only going to be true once the first time.

In Matlab the empty matrix [] can be concatenated with anything and just
takes on it's shape.  It's handy for writing code like the above.

Thanks,
--bill
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20060511/04d8b1d7/attachment.html>


More information about the NumPy-Discussion mailing list