Creating small arrays from strings and concatenating with empty arrays
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
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@noaa.gov
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]])
On the other hand since it's 'matrix', it turns things like "1 2 3" into [[1,2,3]] instead of just [1,2,3]. I think an array version of the matrix string constructor that returns the latter would be handy. But it's admittedly a pretty minor thing. ---bb On 5/12/06, Christopher Barker <Chris.Barker@noaa.gov> wrote:
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@noaa.gov
------------------------------------------------------- Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 _______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/numpy-discussion
-- William V. Baxter III OLM Digital Kono Dens Building Rm 302 1-8-8 Wakabayashi Setagaya-ku Tokyo, Japan 154-0023 +81 (3) 3422-3380
On 5/11/06, Bill Baxter <wbaxter@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]]) Chuck
On 5/13/06, Charles R Harris <charlesr.harris@gmail.com> wrote:
On 5/11/06, Bill Baxter <wbaxter@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
Bill Baxter wrote:
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]])
I think an array version of the matrix string constructor that returns the latter would be handy. But it's admittedly a pretty minor thing.
I agree it's pretty minor indeed, but as long as the code is in the matrix object, why not in the array object as well? As I think about it, I can see two reasons: 1) arrays are n-d. after commas and semi-colons, how do you construct a higher-than-rank-two array? 2) is it really that much harder to type the parentheses?: I suppose there is a bit of inefficiency in creating all those tuples, just to have them dumped, but I can't imagine that ever really matters. By the way, you can do:
a = numpy.fromstring("1 2; 2 3; 3 4", sep=" ").reshape((-1,2)) a array([[1, 2], [2, 3], [3, 4]])
Which, admittedly, is kind of clunky, and, in fact, the ";" is being ignored, but you can put it there to remind yourself what you meant. A note about fromstring/fromfile: I sometimes might have a mix of separators, like the above example. It would be nice I I could pass in more than one that would get used. the above example will only work if there is a space in addition to the semi-colon. It would be nice to be able to do: a = numpy.fromstring("1 2;2 3;3 4", sep=" ;") or a = numpy.fromstring("1,2;2,3;3,4", sep=",;") and have that work. Travis, I believe you said that this code was inspired by my Scanfile code I posted on this list a while back. In that code, I allowed any character that ?scanf didn't interpret as a number be used as a separator: if you asked for the next ten numbers in the file, you'd get the next ten numbers, regardless of what was in between them. While that seems kind of ripe for masking errors, I find that I need to know what the file format I'm working with looks like anyway, and while this approach might mask an error when you read the data, it'll show up soon enough later on, and it sure does make it easy to use and code. Maybe a special string for sep could give us this behavior, like "*" or something. I'm also not sure it's the best idea to put this functionality into fromstring, rather than a separate function, perhaps fromtext()? (or scantext(), or ? ) That's not a big deal, but it just seems like it's a bit hidden there, and scanning a string is a very different operation that interpreting that string as binary data. -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@noaa.gov
participants (3)
-
Bill Baxter
-
Charles R Harris
-
Christopher Barker