Hướng dẫn count occurrences in list python - đếm số lần xuất hiện trong python danh sách

Đưa ra một danh sách trong Python và số X, đếm số lần xuất hiện của X trong danh sách đã cho.

Examples: 

Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10
Output: 3 
Explanation: 10 appears three times in given list.

Input: lst = [8, 6, 8, 10, 8, 20, 10, 8, 8], x = 16
Output: 0
Explanation: 16 appears zero times in given list.

Phương pháp 1: đếm sự xuất hiện của một phần tử trong danh sách sử dụng vòng lặp trong Python

Chúng tôi giữ một bộ đếm tiếp tục tăng nếu phần tử cần thiết được tìm thấy trong danh sách.

Python3

def countX[lst, x]:

8 has occurred 5 times
0
8 has occurred 5 times
1
8 has occurred 5 times
2
8 has occurred 5 times
3

8 has occurred 5 times
0____15
8 has occurred 5 times
6
8 has occurred 5 times
7
8 has occurred 5 times
8

8 has occurred 5 times
9
8 has occurred 5 times 
0
8 has occurred 5 times 
1
8 has occurred 5 times
2
8 has occurred 5 times
2
8 has occurred 5 times 
4

8 has occurred 5 times 
5
8 has occurred 5 times
1
8 has occurred 5 times
2
8 has occurred 5 times
1
8 has occurred 5 times 
9
3 has occurred 2 times
0

8 has occurred 5 times
0
3 has occurred 2 times
2
3 has occurred 2 times
3

Các

1
5
8 has occurred 5 times
2
3 has occurred 2 times
7

1
8
1
9
Element    Count
2            3
1            2
3            2
4            2
5            2
dtype: int64
0
Element    Count
2            3
1            2
3            2
4            2
5            2
dtype: int64
1
Element    Count
2            3
1            2
3            2
4            2
5            2
dtype: int64
2
Element    Count
2            3
1            2
3            2
4            2
5            2
dtype: int64
3

Element    Count
2            3
1            2
3            2
4            2
5            2
dtype: int64
4
Element    Count
2            3
1            2
3            2
4            2
5            2
dtype: int64
5

Output:

8 has occurred 5 times

Phương pháp 2: Số lần xuất hiện của một phần tử trong danh sách bằng cách sử dụng Count []

Ý tưởng là sử dụng số lượng phương thức danh sách [] để đếm số lần xuất hiện. & Nbsp;

Python3

def countX[lst, x]:

8 has occurred 5 times
0
8 has occurred 5 times
1
8 has occurred 5 times
2
8 has occurred 5 times
3

Các

1
5
8 has occurred 5 times
2
3 has occurred 2 times
7

1
8
1
9
Element    Count
2            3
1            2
3            2
4            2
5            2
dtype: int64
0
Element    Count
2            3
1            2
3            2
4            2
5            2
dtype: int64
1
Element    Count
2            3
1            2
3            2
4            2
5            2
dtype: int64
2
Element    Count
2            3
1            2
3            2
4            2
5            2
dtype: int64
3

Element    Count
2            3
1            2
3            2
4            2
5            2
dtype: int64
4
Element    Count
2            3
1            2
3            2
4            2
5            2
dtype: int64
5

Output:

8 has occurred 5 times 

8 has occurred 5 times

Phương pháp 2: Số lần xuất hiện của một phần tử trong danh sách bằng cách sử dụng Count []

Python3

Ý tưởng là sử dụng số lượng phương thức danh sách [] để đếm số lần xuất hiện. & Nbsp;

8 has occurred 5 times
0
3 has occurred 2 times
2
the element 1 occures 2 times
0

Phương pháp 3: Số lần xuất hiện của một phần tử trong danh sách sử dụng Counter []

Phương thức bộ đếm trả về một từ điển với sự xuất hiện của tất cả các phần tử như một cặp giá trị khóa, trong đó khóa là phần tử và giá trị là số lần mà phần tử đã xảy ra. & NBSP;

1
8
1
9
Element    Count
2            3
1            2
3            2
4            2
5            2
dtype: int64
0
Element    Count
2            3
1            2
3            2
4            2
5            2
dtype: int64
1
Element    Count
2            3
1            2
3            2
4            2
5            2
dtype: int64
2
8 has occurred 5 times
41

Output:

3 has occurred 2 times

8 has occurred 5 times
03
8 has occurred 5 times
04
8 has occurred 5 times
05
8 has occurred 5 times
06sing countof[]

Is

Python3

1
5
8 has occurred 5 times
2
8 has occurred 5 times
18

8 has occurred 5 times
33
8 has occurred 5 times
2
8 has occurred 5 times
35

Phương pháp 4: Số lần xuất hiện của một phần tử trong danh sách sử dụng Countof []

1
8
8 has occurred 5 times
73
8 has occurred 5 times
74
8 has occurred 5 times
75

Output:

2 has occurred 3 times

Toán tử.countof [] được sử dụng để đếm số lần xuất hiện của B trong a. Nó đếm số lần xuất hiện của giá trị. Nó trả về số lượng của một số lần xuất hiện của giá trị.

8 has occurred 5 times
05
8 has occurred 5 times
43

Python3

8 has occurred 5 times
07
8 has occurred 5 times
2
3 has occurred 2 times
6
3 has occurred 2 times
0
3 has occurred 2 times
8
3 has occurred 2 times
0
3 has occurred 2 times
8
8 has occurred 5 times
14
3 has occurred 2 times
8
8 has occurred 5 times
14
3 has occurred 2 times
8
8 has occurred 5 times
14
3 has occurred 2 times
8
8 has occurred 5 times
18____38
8 has occurred 5 times
18____________________________________
3 has occurred 2 times
8

1
5
8 has occurred 5 times
2
8 has occurred 5 times
14

1
8
8 has occurred 5 times 
09
8 has occurred 5 times
99
8 has occurred 5 times 
11

Output:

Phương pháp 5: Số lần xuất hiện của một phần tử trong danh sách bằng cách sử dụng từ điển hiểu

Python cho phép sự hiểu biết từ điển. Chúng ta có thể tạo từ điển bằng cách sử dụng các biểu thức đơn giản. Sự hiểu biết từ điển lấy biểu mẫu {key: giá trị cho [khóa, giá trị] trong itable}

8 has occurred 5 times
76
8 has occurred 5 times
2
3 has occurred 2 times
6
8 has occurred 5 times
79
3 has occurred 2 times
8
8 has occurred 5 times
81
3 has occurred 2 times
8__

Python3

8 has occurred 5 times 
01
8 has occurred 5 times
2
8 has occurred 5 times 
03
8 has occurred 5 times
5
8 has occurred 5 times 
05__17
8 has occurred 5 times 
07

8 has occurred 5 times
33
8 has occurred 5 times
2
8 has occurred 5 times
35

Phương pháp 4: Số lần xuất hiện của một phần tử trong danh sách sử dụng Countof []

1
8
1
9
8 has occurred 5 times 
44
8 has occurred 5 times
75

1
8
8 has occurred 5 times 
47

Output:

Element    Count
2            3
1            2
3            2
4            2
5            2
dtype: int64

Toán tử.countof [] được sử dụng để đếm số lần xuất hiện của B trong a. Nó đếm số lần xuất hiện của giá trị. Nó trả về số lượng của một số lần xuất hiện của giá trị.

Python3

8 has occurred 5 times
33
8 has occurred 5 times
2
8 has occurred 5 times
35

8 has occurred 5 times 
73
8 has occurred 5 times
2
3 has occurred 2 times
0

Phương pháp 4: Số lần xuất hiện của một phần tử trong danh sách sử dụng Countof []

1
8
1
9
8 has occurred 5 times 
90
8 has occurred 5 times 
91
8 has occurred 5 times 
92
8 has occurred 5 times 
93
8 has occurred 5 times 
94
8 has occurred 5 times 
95
8 has occurred 5 times 
96
8 has occurred 5 times
75

Đầu ra

the element 1 occures 2 times


Làm thế nào để bạn đếm sự xuất hiện trong một danh sách?

Sử dụng hàm đếm [] theo cách "tiêu chuẩn" [không có thư viện bên ngoài] để có được số lượng từ xuất hiện trong danh sách là sử dụng hàm số [] của đối tượng danh sách.Phương thức Count [] là một hàm tích hợp lấy một phần tử làm đối số duy nhất của nó và trả về số lần phần tử xuất hiện trong danh sách.using the list object's count[] function. The count[] method is a built-in function that takes an element as its only argument and returns the number of times that element appears in the list.

Làm thế nào để bạn đếm số lần một giá trị xuất hiện trong một danh sách?

Đếm tần suất một giá trị duy nhất xảy ra bằng cách sử dụng hàm Countif.Sử dụng chức năng Countif để đếm số lần một giá trị cụ thể xuất hiện trong một loạt các ô.using the COUNTIF function. Use the COUNTIF function to count how many times a particular value appears in a range of cells.

Làm thế nào để bạn đếm số lượng các mục lặp đi lặp lại trong danh sách Python?

Nếu bạn muốn đếm các bản sao cho một phần tử nhất định thì hãy sử dụng hàm đếm [].Sử dụng kết hợp logic chức năng hoặc hàm cơ bản để tìm tất cả các yếu tố trùng lặp trong danh sách và đếm chúng trong Python.use the count[] function. Use a counter[] function or basics logic combination to find all duplicated elements in a list and count them in Python.

Bài Viết Liên Quan

Chủ Đề