Deleting certain elements from numpy array using conditional checks

后端 未结 3 1315
灰色年华
灰色年华 2020-12-31 05:40

I want to remove some entries from a numpy array that is about a million entries long.

This code would do it but take a long time:

a = np.array([1,45         


        
3条回答
  •  无人及你
    2020-12-31 06:32

    You can use masked index with inversed condition.

    >>> a = np.array([1,45,23,23,1234,3432,-1232,-34,233])
    
    >>> a[~((a < -100) | (a > 100))]
    array([  1,  45,  23,  23, -34])
    
    >>> a[(a >= -100) & (a <= 100)]
    array([  1,  45,  23,  23, -34])
    
    >>> a[abs(a) <= 100]
    array([  1,  45,  23,  23, -34])
    

提交回复
热议问题