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

Christopher Barker Chris.Barker at noaa.gov
Thu May 11 09:25:14 EDT 2006


Bill Baxter wrote:
> Two quick questions:
> ---------1------------
> Is there any better way to intialize an array from a string than this:
> 
>    A = asarray(matrix("1 2 3"))

How about:

 >>> import numpy as N
 >>> N.fromstring("1 2 3", sep = " ")
array([1, 2, 3])

or

 >>> N.fromstring("1 2 3", dtype = N.Float, sep = " ")
array([ 1.,  2.,  3.])

If you pass a non-empty "sep" parameter, it parses the string, rather 
than treating is as binary data. fromfile works this way too -- thanks 
Travis!

-Chris


-- 
Christopher Barker, Ph.D.
Oceanographer
                                     		
NOAA/OR&R/HAZMAT         (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov




More information about the NumPy-Discussion mailing list