简体   繁体   English

根据条件从 numpy 数组的子数组中删除行

[英]Removing rows from subarrays of a numpy array based on a condition

I have the following numpy array:我有以下 numpy 数组:

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 .我想在xyz中的xyz中的每一个的第一列中删除值为 10 的行。 So my desired output:所以我想要的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 .我试过xyz[xyz[:,:,0]!=10]但这并不能保留xyz的 3 维性质。 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.我想我可以将第一个维度、切片和 append 迭代到一个新数组,但我正在寻找一个更简单(可能是单线)的解决方案。

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.但是,由于尺寸不匹配,这不再是真正的 3D numpy 数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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