简体   繁体   English

根据每个数组的特定索引中的值,在 2D numpy 数组中按索引查找和切片?

[英]Find and slice by indices in 2D numpy array where according to value in certain index of each array?

I have many 2D numpy arrays that look like this:我有很多 2D numpy arrays 看起来像这样:

arr = np.array([[2,2],
                [2,3],
                [3,4],
                [3,5],
                [3,6],
                [4,7]))

How can I query the 2D array and retrieve all the arrays with a value of, for example, 3 in the 0 index?如何查询二维数组并检索 0 索引中所有值为 3 的 arrays? So I would want:所以我想要:

[[3,4],[3,5],[3,6]]

I considered turning it into a list of lists, but that seems inefficient as I have a lot of queries to make.我考虑过将它变成列表列表,但这似乎效率低下,因为我有很多查询要提出。 Using np.argwhere or np.where doesn't seem to isolate by index value, either.使用 np.argwhere 或 np.where 似乎也不按索引值进行隔离。

Thank you.谢谢你。

For advanced indexing, at first we must find elements which have the specified number as its first argument by arr[:, 0] == 3 , so:对于高级索引,首先我们必须通过arr[:, 0] == 3找到具有指定编号作为其第一个参数的元素,因此:

arr[arr[:, 0] == 3]

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

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