Home  >  Article  >  Backend Development  >  The difference between size and count in python

The difference between size and count in python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-24 15:11:295453browse

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.

The difference between size and count in python

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) :  1

count(): 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

Related recommendations: "

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)
2

The 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 1

size() 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.

size can be used as a function or as an attribute of ndarray.

a.size, the return value is 6.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn