[Numpy-discussion] Excluding index in numpy like negative index in R?

Bab Tei babaktei@yahoo....
Wed Dec 10 16:30:43 CST 2008


Keith Goodman <kwgoodman <at> gmail.com> writes:

> 
> On Tue, Dec 9, 2008 at 12:25 PM, Bab Tei <babaktei <at> yahoo.com> wrote:
> 
> > I can exclude a list of items by using negative index in R (R-project) ie
myarray[-excludeindex]. As
> negative indexing in numpy (And python) behave differently ,how can I exclude
a list of item in numpy?
> 
> Here's a painful way to do it:
> 
> >> x = np.array([0,1,2,3,4])
> >> excludeindex = [1,3]
> >> idx = list(set(range(4)) - set(excludeindex))
> >> x[idx]
>    array([0, 2])
> 
> To make it more painful, you might want to sort idx.
> 
> But if excludeindex is True/False, then just use ~excludeindex.
> 


Thank you. However it seems I have to create a full list at first and then
exclude items. It is somehow painful as I have some very large sparse matrices
and creating a full index eats a lot of memory. Maybe adding this functionality
to numpy saves memory and makes the syntax more clear ie a syntax like
x[~excludeindex] which smartly distinguish between excludeindex as a list of
numerical indexes and a mask (list of true/false indexes).
Regards


      


More information about the Numpy-discussion mailing list