Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

NumPy: count() function

numpy.core.defchararray.count() function

The numpy.core.defchararray.count() function returns an array with the number of non-overlapping occurrences of substring sub in the range [start, end].

Version: 1.15.0

Syntax:

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

Parameter:

Name Description Required /
Optional
a: array_like of str or unicode Input an array_like of string or unicode. Required
sub: str or unicode The substring to search for. Required
start, end: int Optional arguments start and end are interpreted as slice notation to specify the range in which to count. Optional

Return value:

out : ndarray - Output array of ints.

Example-1: numpy.count() function


>>> c = np.array(['aAaAaA', '  aA  ', 'abBABba'])
>>> c
array(['aAaAaA', '  aA  ', 'abBABba'],
    dtype='|S7')
>>> 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])

Pictorial Presentation:

NumPy String operation: count() function

Pictorial Presentation:

NumPy String operation: count() function

Python - NumPy Code Editor:

Previous: zfill()
Next: find()