[Numpy-discussion] Creating and reshaping fortran order arrays

Fernando Perez fperez.net at gmail.com
Mon Aug 14 23:06:29 CDT 2006


On 8/14/06, Travis Oliphant <oliphant.travis at ieee.org> wrote:
> Travis Oliphant wrote:
> > 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.
> >
> Incidentally, the new array will be read-only.  But, you can fix this in
> two ways:
>
> 1) a.flags.writeable = True

Sweet! We now finally have mutable strings for Python:

In [2]: astr = '\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00'

In [4]: a = N.ndarray(shape=(2,2), dtype=N.int32, buffer=astr, order='F')

In [5]: astr
Out[5]: '\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00'

In [6]: a.flags.writeable = True

In [7]: a
Out[7]:
array([[0, 2],
       [1, 3]])

In [8]: a[0] = 1

In [9]: astr
Out[9]: '\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x03\x00\x00\x00'


Guido's going to kill you on Thursday, you know ;)

f




More information about the Numpy-discussion mailing list