创建一个布尔数组,numpy的元素进行比较,以无 [英] creating a boolean array which compares numpy elements to None

查看:115
本文介绍了创建一个布尔数组,numpy的元素进行比较,以无的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有DTYPE =对象的数组numpy的,我想创建一个布尔数组确定哪些元素是。但它看起来像行为不同...

I have a numpy array with dtype=object, and I want to create a boolean array identifying which elements are None. But it looks like None behaves differently...

a = np.array(['Duck','Duck','Duck','Goose',None,1,2,3,1,3,None,4])
print a == 'Duck'
print a == 3
print a == None

这导致

[ True  True  True False False False False False False False False False]
[False False False False False False False  True False  True False False]
False

是否有一个numpythonic的方式来获得元素的布尔数组?我可以使用

Is there an "numpythonic" way to get a boolean array of the None elements? I can use

np.array([x is None for x in a])

不过这似乎应该有一个更好的办法。

but this seems like there should be a better way.

推荐答案

您可以使用<一个href=\"http://docs.scipy.org/doc/numpy/reference/generated/numpy.equal.html\"><$c$c>numpy.equal:

In [20]: np.equal(a, None)
Out[20]: 
array([False, False, False, False,  True, False, False, False, False,
       False,  True, False], dtype=bool)

这篇关于创建一个布尔数组,numpy的元素进行比较,以无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆