Home > Article > Backend Development > How to count the number of elements in a list in python
When using Python, the following scenarios usually occur:
For example, in the above list: 0 appears once , 1 appears 2 times, 2 appears 3 times, and 3 appears 2 times.
This article explains several methods for obtaining the number of occurrences of elements in Python.
There are many ways to get the number of occurrences of an element. Here I propose the following methods. In the following code, the parameters passed in are array = [1, 2, 3, 3, 2, 1 , 0, 2]
conuter() method
This method can quickly obtain the elements in the list The number of occurrences
count in the list, get the number of occurrences of each element
#Numpy fancy index , get the number of occurrences of each element
##
The above is the detailed content of How to count the number of elements in a list in python. For more information, please follow other related articles on the PHP Chinese website!