[SciPy-user] Quick way to delete all 'values' > from array

David Goldsmith d_l_goldsmith at yahoo.com
Tue Jul 14 16:19:21 EDT 2009


> From: Scott Sinclair <scott.sinclair.za at gmail.com>
> Subject: Re: [SciPy-user] Quick way to delete all 'values'
> from array

<snip>

> If by 'quickest' you mean 'easiest'. Here's how to do it
> using fancy indexing:
> 
> >>> import numpy as np
> >>> a = np.array([0, -1, 2, 3, -1, 4])
> >>> a
> array([ 0, -1,  2,  3, -1,  4])
> >>> a = a[a != -1]
> >>> a
> array([0, 2, 3, 4])

what if he wants to eliminate more than one value at a time; is that possible?  I tried a bunch of ways, the closest I got (methinks) to success was:

>>> a[[index for index in range(len(a)) a[index] not in (0,2)]]

That gave an invalid syntax error at the third a; using a colon following the len(a)) gave an invalid syntax error at the colon; and using a comma after len(a)) gave a NameError: name 'index' not defined.  Am I just forgetting how to do conditional list comprehension, and/or is this approach doomed to failure anyway?  If the latter, is there an alternative way to do this?

Curious,
DG


      



More information about the SciPy-User mailing list