在 numpy 数组中查找包含子字符串的条目? [英] Finding entries containing a substring in a numpy array?

查看:163
本文介绍了在 numpy 数组中查找包含子字符串的条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在包含带有 np.where 和 in 条件的子字符串的数组中查找条目:

I tried to find entries in an Array containing a substring with np.where and an in condition:

import numpy as np
foo = "aa"
bar = np.array(["aaa", "aab", "aca"])
np.where(foo in bar)

这只会返回一个空数组.
为什么会这样?
有没有好的替代解决方案?

this only returns an empty Array.
Why is that so?
And is there a good alternative solution?

推荐答案

我们可以使用 np.core.defchararray.find 查找foo字符串在bar<的每个元素中的位置/code>,如果没有找到,它会返回 -1.因此,它可用于通过检查 find 输出中的 -1 来检测每个元素中是否存在 foo.最后,我们将使用 np.flatnonzero 来获取匹配的索引.所以,我们会有一个实现,就像这样 -

We can use np.core.defchararray.find to find the position of foo string in each element of bar, which would return -1 if not found. Thus, it could be used to detect whether foo is present in each element or not by checking for -1 on the output from find. Finally, we would use np.flatnonzero to get the indices of matches. So, we would have an implementation, like so -

np.flatnonzero(np.core.defchararray.find(bar,foo)!=-1)

样品运行 -

In [91]: bar
Out[91]: 
array(['aaa', 'aab', 'aca'], 
      dtype='|S3')

In [92]: foo
Out[92]: 'aa'

In [93]: np.flatnonzero(np.core.defchararray.find(bar,foo)!=-1)
Out[93]: array([0, 1])

In [94]: bar[2] = 'jaa'

In [95]: np.flatnonzero(np.core.defchararray.find(bar,foo)!=-1)
Out[95]: array([0, 1, 2])

这篇关于在 numpy 数组中查找包含子字符串的条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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