简体   繁体   English

如何在多列上屏蔽 numpy 结构化数组?

[英]How to mask numpy structured array on multiple columns?

I have a numpy structured array with a dtype such as:我有一个 Dtype 的 numpy 结构化数组,例如:

A = numpy.empty(10, dtype=([('segment', '<i8'), ('material', '<i8'), ('rxN', '<i8')]))

I know I can create a mask such as:我知道我可以创建一个面具,例如:

A[A['segment'] == 42] = ...

Is there a way to create a mask on multiple columns?有没有办法在多列上创建掩码? For example (I know this doesn't work, but I wish it did):例如(我知道这不起作用,但我希望它起作用):

A[A['segment'] == 42 and A['material'] == 5] = ...

You can use the & operator instead of and :您可以使用&运算符代替and

A[(A['segment'] == 42) & (A['material'] == 5)]

Note that extra parantheses are required.请注意,需要额外的括号。

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

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