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

Pierre GM pgmdevlist at gmail.com
Tue Jul 14 16:49:50 EDT 2009


On Jul 14, 2009, at 4:19 PM, David Goldsmith wrote:
>
> what if he wants to eliminate more than one value at a time; is that  
> possible?

David,
Put it that way: a ndarray occupies a fixed space in memory (set when  
you create), and you cannot add nor delete entries. What you can do is  
to create a *new* array that contains only the values that satisfy a  
given condition. It's what we're doing with the syntax
 >>> a[a!=1].
Now, if you have several entries that you want to discard, you can try  
to create a boolean array that satisfies all the conditions  
simulatenously, like
 >>> cond=np.logical_and.reduce([a!=_ for _ in (1,2)])
and use that to select the proper values
 >>> a[cond]


>  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
>
>
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user




More information about the SciPy-User mailing list