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

Bill Baxter wbaxter at gmail.com
Mon May 15 18:10:22 EDT 2006


On 5/13/06, Charles R Harris <charlesr.harris at gmail.com> wrote:
>
>
>
> On 5/11/06, Bill Baxter <wbaxter at gmail.com> wrote:
> >
> > Ahh, I hadn't noticed the fromstring/fromfile methods.
> >
> > Hmm.  That seems ok for making a row-at-a-time, but it doesn't support
> > the full syntax of the matrix string constructor,  which allows for things
> > like
> >
> > >>> numpy.matrix("[1 2; 2 3;3 4]")
> > matrix([[1, 2],
> >        [2, 3],
> >        [3, 4]])
> >
>
> You can reshape the array returned by fromstring, i.e.,
>
> In [6]: fromstring("1 2 2 3 3 4", sep=" ").reshape(-1,2)
> Out[6]:
> array([[1, 2],
>        [2, 3],
>        [3, 4]])
>

But if the string comes from someplace other than a literal right there in
the code (like loaded from a file or passed in as an argument or something),
I may not know the shape in advance.  I'll just stick with the matrix
constructor, since for my case, I do know the array dim is 2.

--bill
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20060515/80f40238/attachment-0001.html>


More information about the NumPy-Discussion mailing list