[Numpy-discussion] Creating and reshaping fortran order arrays

Travis Oliphant oliphant.travis at ieee.org
Mon Aug 14 12:55:40 CDT 2006


Matthew Brett wrote:
> Hi,
>
> I am sorry if this is obvious, but:
>   

It's O.K. I don't think many people are used to the fortran-order 
stuff.  So, I doubt it's obvious.
> For example, here is 0,1,2,3 as int32
>
> str = '\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00'
>
> What is the best way of me putting this into a 2x2 array object so
> that the array recognizes the data is in fortran order.  Sort of:
>   
> a = somefunction(str, shape=(2,2), dtype=int32, order='F')
>   
There isn't really a function like this because the fromstring function 
only creates 1-d arrays that must be reshaped later (it also copies the 
data from the string).

However, you can use the ndarray creation function itself to do what you 
want:

a = ndarray(shape=(2,2), dtype=int32, buffer=str, order='F')

This will use the memory of the string as the new array memory.


-Travis





More information about the Numpy-discussion mailing list