Look at some examples of using in
:
In [19]: bar = np.array(["aaa", "aab", "aca"])
In [20]: 'aa' in bar
Out[20]: False
In [21]: 'aaa' in bar
Out[21]: True
In [22]: 'aab' in bar
Out[22]: True
In [23]: 'aab' in list(bar)
It looks like in
when used with an array works as though the array was a list. ndarray
does have a __contains__
method, so in
works, but it is probably simple.
But in any case, note that in alist
does not check for substrings. The strings
__contains__
does the substring test, but I don't know any builtin class that propagates the test down to the component strings.
As Divakar
shows there is a collection of numpy functions that applies string methods to individual elements of an array.
In [42]: np.char.find(bar, 'aa')
Out[42]: array([ 0, 0, -1])
Docstring:
This module contains a set of functions for vectorized string
operations and methods.
The preferred alias for defchararray
is numpy.char
.
For operations like this I think the np.char
speeds are about same as with:
In [49]: np.frompyfunc(lambda x: x.find('aa'), 1, 1)(bar)
Out[49]: array([0, 0, -1], dtype=object)
In [50]: np.frompyfunc(lambda x: 'aa' in x, 1, 1)(bar)
Out[50]: array([True, True, False], dtype=object)
Further tests suggest that the ndarray
__contains__
operates on the flat
version of the array - that is, shape doesn't affect its behavior.