[Numpy-discussion] 'append' array method request.
Sasha
ndarray at mac.com
Fri Apr 21 15:20:01 CDT 2006
On 4/21/06, Robert Hetland <hetland at tamu.edu> wrote:
> [...]
> I think it would be nice to be able to create empty arrays, and
> append the values onto the end as I loop through the file without
> creating the intermediate list. Is this reasonable? Is there a way
> to do this with existing methods or functions that I am missing? Is
> there a better way altogether?
>
Numpy arrays cannot grow in-place because there is no way for an array
to tell if it's data is shared with other arrays. You can use
python's standard library arrays instead of lists:
>>> from numpy import *
>>> import array as a
>>> x = a.array('i',[])
>>> x.append(1)
>>> x.append(2)
>>> x.append(3)
>>> ndarray(len(x), dtype=int, buffer=x)
array([1, 2, 3])
Note that data is not copied:
>>> ndarray(len(x), dtype=int, buffer=x)[1] = 20
>>> x
array('i', [1, 20, 3])
More information about the Numpy-discussion
mailing list