Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

python - 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.

question from:https://stackoverflow.com/questions/65889016/removing-rows-from-subarrays-of-a-numpy-array-based-on-a-condition

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...