Đư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 times0
8 has occurred 5 times1
8 has occurred 5 times2
8 has occurred 5 times3
8 has occurred 5 times0____15
8 has occurred 5 times6
8 has occurred 5 times7
8 has occurred 5 times8
8 has occurred 5 times9
8 has occurred 5 times0
8 has occurred 5 times1
8 has occurred 5 times2
8 has occurred 5 times2
8 has occurred 5 times4
8 has occurred 5 times5
8 has occurred 5 times1
8 has occurred 5 times2
8 has occurred 5 times1
8 has occurred 5 times9
3 has occurred 2 times0
8 has occurred 5 times0
3 has occurred 2 times2
3 has occurred 2 times3
Các
15
8 has occurred 5 times2
3 has occurred 2 times7
18
19
Element Count 2 3 1 2 3 2 4 2 5 2 dtype: int640
Element Count 2 3 1 2 3 2 4 2 5 2 dtype: int641
Element Count 2 3 1 2 3 2 4 2 5 2 dtype: int642
Element Count 2 3 1 2 3 2 4 2 5 2 dtype: int643
Element Count 2 3 1 2 3 2 4 2 5 2 dtype: int644
Element Count 2 3 1 2 3 2 4 2 5 2 dtype: int645
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 times0
8 has occurred 5 times1
8 has occurred 5 times2
8 has occurred 5 times3
Các
15
8 has occurred 5 times2
3 has occurred 2 times7
18
19
Element Count 2 3 1 2 3 2 4 2 5 2 dtype: int640
Element Count 2 3 1 2 3 2 4 2 5 2 dtype: int641
Element Count 2 3 1 2 3 2 4 2 5 2 dtype: int642
Element Count 2 3 1 2 3 2 4 2 5 2 dtype: int643
Element Count 2 3 1 2 3 2 4 2 5 2 dtype: int644
Element Count 2 3 1 2 3 2 4 2 5 2 dtype: int645
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 times0
3 has occurred 2 times2
the element 1 occures 2 times0
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;
18
19
Element Count 2 3 1 2 3 2 4 2 5 2 dtype: int640
Element Count 2 3 1 2 3 2 4 2 5 2 dtype: int641
Element Count 2 3 1 2 3 2 4 2 5 2 dtype: int642
8 has occurred 5 times41
Output:
3 has occurred 2 times
8 has occurred 5 times
03 8 has occurred 5 times
048 has occurred 5 times
05 8 has occurred 5 times
06sing countof[]
Is
Python3
15
8 has occurred 5 times2
8 has occurred 5 times18
8 has occurred 5 times33
8 has occurred 5 times2
8 has occurred 5 times35
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 []
18
8 has occurred 5 times73
8 has occurred 5 times74
8 has occurred 5 times75
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 times05
8 has occurred 5 times43
Python3
8 has occurred 5 times07
8 has occurred 5 times2
3 has occurred 2 times6
3 has occurred 2 times0
3 has occurred 2 times8
3 has occurred 2 times0
3 has occurred 2 times8
8 has occurred 5 times14
3 has occurred 2 times8
8 has occurred 5 times14
3 has occurred 2 times8
8 has occurred 5 times14
3 has occurred 2 times8
8 has occurred 5 times18____38
8 has occurred 5 times18____________________________________
3 has occurred 2 times8
15
8 has occurred 5 times2
8 has occurred 5 times14
18
8 has occurred 5 times09
8 has occurred 5 times99
8 has occurred 5 times11
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ểuPython 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 times76
8 has occurred 5 times2
3 has occurred 2 times6
8 has occurred 5 times79
3 has occurred 2 times8
8 has occurred 5 times81
3 has occurred 2 times8__
Python3
8 has occurred 5 times01
8 has occurred 5 times2
8 has occurred 5 times03
8 has occurred 5 times5
8 has occurred 5 times05__17
8 has occurred 5 times07
8 has occurred 5 times33
8 has occurred 5 times2
8 has occurred 5 times35
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 []
18
19
8 has occurred 5 times44
8 has occurred 5 times75
18
8 has occurred 5 times47
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 times33
8 has occurred 5 times2
8 has occurred 5 times35
8 has occurred 5 times73
8 has occurred 5 times2
3 has occurred 2 times0
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 []
18
19
8 has occurred 5 times90
8 has occurred 5 times91
8 has occurred 5 times92
8 has occurred 5 times93
8 has occurred 5 times94
8 has occurred 5 times95
8 has occurred 5 times96
8 has occurred 5 times75
Đầu ra
the element 1 occures 2 times