Home >Backend Development >Python Tutorial >The difference between size and count in python
The count() method in Python is used to count the number of times a certain character appears in a string. Optional parameters are the start and end positions of the string search. count() method syntax:
str.count(sub, start= 0,end=len(string)); the size() function is mainly used to count the number of matrix elements, or a certain dimension of the matrix function of the number of elements.
count()
##Parameters
sub -- The search substring start -- The position in the string where the search begins. The default is the first character, and the index value of the first character is 0. end – The position in the string where the search ends. The first character in the string has index 0. Defaults to the last position of the string.Return value
This method returns the number of times the substring appears in the string.Examples
The following examples show examples of the count() method:#!/usr/bin/python str = "this is string example....wow!!!"; sub = "i"; print "str.count(sub, 4, 40) : ", str.count(sub, 4, 40) sub = "wow"; print "str.count(sub) : ", str.count(sub)The output results of the above examples are as follows:
str.count(sub, 4, 40) : 2 str.count(sub, 4, 40) : 1count(): Calculate the number of objects contained
[1,1,1,2].count(1), the return value is 3
'asddf'.count('d'), the return value is 2
Python Video Tutorial"
size()
Parameters
numpy.size(a, axis=None)a: Input matrix axis: Optional parameter of type int, specifying the number of elements in which dimension to return. When not specified, returns the number of elements of the entire matrix. Example
>>> a = np.array([[1,2,3],[4,5,6]]) >>> np.size(a) 6 >>> np.size(a,1) 3 >>> np.size(a,0) 2The value of axis is not set, and the number of elements of the matrix is returned: axis = 0, the number of rows of the two-dimensional matrix is returned, axis = 1, the number of rows of the two-dimensional matrix is returned number of columns. Note: The second parameter axis starts from 0, not from 1size() is a function only in the numpy module. size(): Calculate the number of all data in arrays and matrices.
a = np.array([[1,2,3],[4,5,6]])
np.size(a), the return value is 6.
np.size(a,1), the return value is 3.
The above is the detailed content of The difference between size and count in python. For more information, please follow other related articles on the PHP Chinese website!