Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我有一个 Nx3 数组,我想要满足的元素说:
4 < col1 < 13, 5 > col2 > 3, 10 > col3 > 6
即适用于:
1,2,3 4,5,6 9,4,7
然后它将提供一个 Mx3 数组,其中仅包含满足所有三个条件的行。对于上面的例子,它输出
9,4,7
我想过做一个循环,但我认为 numpy 或类似的东西必须有更快的东西(例如 np.where?)
您为每一列构建一个布尔数组:
cond1 = (my_array[:, 0] > 4) & (my_array[:, 0] < 13) cond2 = (my_array[:, 1] > 3) & (my_array[:, 1] < 5) cond3 = (my_array[:, 2] > 6) & (my_array[:, 2] < 10)
然后and你把你的三个条件放在一起:
and
cond = cond1 & cond2 & cond3
最后,您使用它从数组中选择行:
out = my_array[cond]