我有一个如下的结构:
product_type = np.dtype([('message_counter', np.int),
('alteration_time', 'U32'),
('area_states', status_type, (3,)),
])
使用:
status_type = np.dtype([('area', 'U32'),
('state', 'U32')])
此外,我还有一个product_type
数组,比如:
products = np.array([product1, product2, ...], dtype=product_type)
现在我想选择只具有status_type
等于('area1', 'active')
的产品。我将如何做到这一点。我试过这样的方法:
mask = np.isin(products['area_states'][['area', 'state']],
('area1', 'active'))
active_products = products[mask]
不幸的是,这并不是我所希望的那样。当然,我只收到了子数组(status_type
)的掩码,但是我更喜欢在产品上得到一个掩码,这样我就可以过滤只有status_type
和('area1', 'active')
的产品。你知道吗
所有的代码都是这样的:
status_type = np.dtype([('area', 'U32'),
('state', 'U32')])
product_type = np.dtype([('message_counter', np.int),
('alteration_time', 'U32'),
('area_states', status_type, (3,)),
])
products = np.array([(253, '12:00', [('area1', 'active'), ('area2', 'inactive'), ('area3', 'inactive')]),
(254, '13:00', [('area1', 'inactive'), ('area2', 'inactive'), ('area3', 'inactive')])],
dtype=product_type)
active_products_in_area1 = '???'
可以使用创建目标状态变量
并使用
np.any
获得活动产品掩码(通过status_type
列表沿轴1循环减少)它只生成示例数组中的第一条记录:
相关问题 更多 >
编程相关推荐