[SciPy-User] [scipy-user] How to remove a value from an np array?
Tue Jan 31 10:54:04 CST 2012
On 27.01.2012 19:20, David Warde-Farley wrote:
> This is generally not something you want to be doing with NumPy arrays. NumPy
> arrays represent fixed blocks of memory, removing an element means
> reallocating the array and copying values before and after position 8 is the
> only way.
> If a is one-dimensional, then np.concatenate(a[:8], a[9:]) will do the job,
> but if you are going to be frequently performing this kind of operation you
> will get better performance out of a plain Python list.
Python lists are also implemented as arrays. Removing an element will be
O(N) in both cases, except for the tail of a Python list where removal
Also a linked list will not help here. Remving an element would be O(1),
but looking it up would be O(N). That is why Python's collections.deque
will not help, even though it is a linked list of short arrays.
An alternative to removing elements is using NumPy's masked array.
Setting one up would be O(n), but once it is allocated, "removing"
elements by masking them out is O(1). It is the fastest data structure
if you need to remove (or "mask out") arbitrary elements from an array.
More information about the SciPy-User