efficiently create and fill array.array from C code?
Martin
mdekauwe at gmail.com
Sun Jun 13 16:36:35 EDT 2010
On Jun 13, 6:15 pm, Thomas Jollans <tho... at jollans.com> wrote:
> Hi,
>
> I'm writing some buffer-centric number-crunching routines in C for
> Python code that uses array.array objects for storing/manipulating data.
> I would like to:
>
> 1. allocate a buffer of a certain size
> 2. fill it
> 3. return it as an array.
>
> I can't see any obvious way to do this with the array module, but I was
> hoping somebody here might be able to help. My best shot would be to:
>
> 1. create a bytearray with PyByteArray_FromStringAndSize(NULL, byte_len)
> 2. fill its buffer
> 3. initialize an array from the bytearray.
>
> The issue I have with this approach is that array will copy the data to
> its own buffer. I'd much rather create an array of a certain size, get a
> write buffer, and fill it directly -- is that possible?
>
> I expect that numpy allows this, but I don't really want to depend on
> numpy, especially as they haven't released a py3k version yet.
>
> -- Thomas
You want Numpy...
e.g.
import numpy as np
array = np.zeros(100, dtype=np.uint8)
then either something like this to fill it
for i in xrange(len(100)):
array[i] = 2
or
array = np.zeros(0)
for i in xrange(len(100)):
array = np.append(array, 2)
Mart
More information about the Python-list
mailing list