NumPy

Previous topic

numpy.char.compare_chararrays

Next topic

numpy.char.endswith

This is documentation for an old release of NumPy (version 1.18). Read this page in the documentation of the latest stable release (version 2.2).

numpy.char.count

numpy.char.count(a, sub, start=0, end=None)

Returns an array with the number of non-overlapping occurrences of substring sub in the range [start, end].

Calls str.count element-wise.

Parameters
aarray_like of str or unicode
substr or unicode

The substring to search for.

start, endint, optional

Optional arguments start and end are interpreted as slice notation to specify the range in which to count.

Returns
outndarray

Output array of ints.

See also

str.count

Examples

>>>
>>> c = np.array(['aAaAaA', '  aA  ', 'abBABba'])
>>> c
array(['aAaAaA', '  aA  ', 'abBABba'], dtype='<U7')
>>> np.char.count(c, 'A')
array([3, 1, 1])
>>> np.char.count(c, 'aA')
array([3, 1, 0])
>>> np.char.count(c, 'A', start=1, end=4)
array([2, 1, 1])
>>> np.char.count(c, 'A', start=1, end=3)
array([1, 0, 0])