简体   繁体   中英

Removing rows from subarrays of a numpy array based on a condition

I have the following numpy array:

x = [[1,2],[3,4],[10,1]]
y = [[5,6],[1,8],[7,8]]
z = [[10,2],[9,10],[11,12]]
xyz = np.array([x,y,z])

I want to remove rows with value 10 in the first column of each of x , y , z within xyz . So my desired output:

array([[[ 1,  2],
        [ 3,  4]],

       [[ 5,  6],
        [ 1,  8],
        [ 7,  8]],

       [[ 9, 10],
        [11, 12]]], dtype=object)

I tried xyz[xyz[:,:,0]!=10] but this doesn't preserve the 3-dimensional nature of xyz . I guess I can iterate over the first dimension, slice and append to a new array but I'm looking for a simpler (possibly a one-liner) solution.

Try:

np.array([a[a[:,0]!=10] for a in xyz])

But this, due to mismatch dimension, is not really a 3D numpy array anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM