The numpy.char.count()
function in Python returns an output array with the number of non-overlapping occurrences of a substring in a given range [start
, end
] from an input array a
.
char. count(a, sub, start=0, end=None)
The char.count()
function takes the following parameter values:
a
: This is the input array.sub
: This is the substring to search for.start
, end
: This is the slice notation specifying the range, in each element of the given array, where the counting is done.The char.count()
function returns an array of integers with the same shape as the input array.
import numpy as np# Creating the input arraya = np.array(['bBbABAbB', 'bB', 'bCBcAcb'])# Creating the substring to search forsub = "A"# Implmenting the char.count() functionmyarray = np.char.count(a, sub, start=1, end=5)# Printing the input and output arraysprint(a)print(myarray)
numpy
module.a
using the array()
function.sub
to search for.char.count()
function on the input array, and search for the number of occurrences of a
in the array elements. The result is assigned to a variable myarray
.a
. myarray
.