numpy.count() in Python (original) (raw)
Last Updated : 10 Oct, 2019
numpy.core.defchararray.count(arr, substring, start=0, end=None)
: Counts for the non-overlapping occurrence of sub-string in the specified range.
Parameters:
arr : array-like or string to be searched.
substring : substring to search for.
start, end : [int, optional] Range to search in.Returns : An integer array with the number of non-overlapping occurrences of sub-string.
Code #1:
import
numpy as np
arr
=
[
'vdsdsttetteteAAAa'
,
'AAAAAAAaattttds'
,
'AAaaxxxxtt'
,
'AAaaXDSDdscz'
]
print
(
"arr : "
, arr)
print
(
"Count of 'tt'"
, np.char.count(arr,
'tt'
))
print
(
"Count of 'tt'"
, np.char.count(arr,
'tt'
, start
=
0
))
print
(
"Count of 'tt'"
, np.char.count(arr,
'tt'
, start
=
8
))
Output:
arr : ['vdsdsttetteteAAAa', 'AAAAAAAaattttds', 'AAaaxxxxtt', 'AAaaXDSDdscz'] Count of 'tt' [2 2 1 0] Count of 'tt' [2 2 1 0] Count of 'tt' [1 2 1 0]
Code #2:
import
numpy as np
arr
=
[
'vdsdsttetteteAAAa'
,
'AAAAAAAaattttds'
,
'AAaaxxxxtt'
,
'AAaaXDSDdscz'
]
print
(
"arr : "
, arr)
print
(
"Count of 'Aa'"
, np.char.count(arr,
'Aa'
))
print
(
"Count of 'Aa'"
, np.char.count(arr,
'Aa'
, start
=
8
))
Output:
arr : ['vdsdsttetteteAAAa', 'AAAAAAAaattttds', 'AAaaxxxxtt', 'AAaaXDSDdscz']
Count of 'Aa' [1 1 1 1] Count of 'Aa' [1 0 0 0]
Similar Reads
- numpy.bincount() in Python In an array of +ve integers, the numpy.bincount() method counts the occurrence of each element. Each bin value is the occurrence of its index. One can also set the bin size accordingly. Syntax : numpy.bincount(arr, weights = None, min_len = 0) Parameters : arr : [array_like, 1D]Input array, having p 2 min read
- numpy.any() in Python The numpy.any() function tests whether any array elements along the mentioned axis evaluate to True. Syntax : numpy.any(a, axis = None, out = None, keepdims = class numpy._globals._NoValue at 0x40ba726c) Parameters : array :[array_like]Input array or object whose elements, we need to test. axis : [i 3 min read
- numpy.all() in Python The numpy.all() function tests whether all array elements along the mentioned axis evaluate to True. Syntax: numpy.all(array, axis = None, out = None, keepdims = class numpy._globals._NoValue at 0x40ba726c) Parameters : array :[array_like]Input array or object whose elements, we need to test. axis : 3 min read
- Numpy size() function | Python numpy.size() function in Python is used to count the number of elements in a NumPy array. You can use it to get the total count of all elements, or to count elements along a specific axis, such as rows or columns in a multidimensional array. This makes it useful when quickly trying to understand the 2 min read
- NumPy Array in Python NumPy (Numerical Python) is a powerful library for numerical computations in Python. It is commonly referred to multidimensional container that holds the same data type. It is the core data structure of the NumPy library and is optimized for numerical and scientific computation in Python. Table of C 2 min read
- Numpy count_nonzero method | Python numpy.count_nonzero() function counts the number of non-zero values in the array arr. Syntax : numpy.count_nonzero(arr, axis=None) Parameters : arr : [array_like] The array for which to count non-zeros. axis : [int or tuple, optional] Axis or tuple of axes along which to count non-zeros. Default is 1 min read
- numpy.nansum() in Python numpy.nansum()function is used when we want to compute the sum of array elements over a given axis treating Not a Numbers (NaNs) as zero. Syntax : numpy.nansum(arr, axis=None, dtype=None, out=None, keepdims='no value') Parameters : arr : [array_like] Array containing numbers whose sum is desired. If 3 min read
- numpy.nonzero() in Python numpy.nonzero()function is used to Compute the indices of the elements that are non-zero. It returns a tuple of arrays, one for each dimension of arr, containing the indices of the non-zero elements in that dimension. The corresponding non-zero values in the array can be obtained with arr[nonzero(ar 2 min read
- numpy.fromstring() function – Python numpy.fromstring() function create a new one-dimensional array initialized from text data in a string. Syntax : numpy.fromstring(string, dtype = float, count = -1, sep = ' ') Parameters : string : [str] A string that contained the data. dtype : [data-type, optional] Data-type of the array. Default d 1 min read
- Python List count() method The count() method is used to find the number of times a specific element occurs in a list. It is very useful in scenarios where we need to perform frequency analysis on the data. Let's look at a basic example of the count() method. [GFGTABS] Python a = [1, 2, 3, 1, 2, 1, 4] c = a.count(1) print(c) 2 min read