Hướng dẫn how do you count the number of times a letter appears in a list python? - làm thế nào để bạn đếm số lần một chữ cái xuất hiện trong một python danh sách?

Được rồi, đây là một danh sách:

sentList = ['I am a dog', 'I am a cat', 'I am a house full of cards']

Tôi muốn có thể đếm tổng số lần một chữ cái được nhập của người dùng xuất hiện trong toàn bộ danh sách.

userLetter = input['Enter a letter: ']

Giả sử bức thư là 'A'

Tôi muốn chương trình đi qua và đếm số lần 'A' xuất hiện trong danh sách. Trong trường hợp này, tổng số 'A' trong danh sách phải là 8.

Tôi đã thử sử dụng chức năng đếm thông qua một vòng lặp nhưng tôi tiếp tục nhận được các số mà tôi không biết cách giải thích và không thực sự biết cách định dạng vòng lặp, hoặc nếu tôi cần nó.

Tôi đã thử điều này, nhưng nó không hoạt động.

count = sentList.count[userLetter]

Bất kỳ trợ giúp nào cũng được đánh giá cao, tôi không thể tìm thấy bất kỳ tài liệu nào để đếm tất cả các lần xuất hiện của một lá thư trong danh sách.

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?

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.

Đế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 ô.

Đư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.

Python3

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.

userLetter = input['Enter a letter: ']
1
userLetter = input['Enter a letter: ']
2

userLetter = input['Enter a letter: ']
3
userLetter = input['Enter a letter: ']
4
userLetter = input['Enter a letter: ']
5
userLetter = input['Enter a letter: ']
6

userLetter = input['Enter a letter: ']
3
userLetter = input['Enter a letter: ']
8
userLetter = input['Enter a letter: ']
9
count = sentList.count[userLetter]
0
count = sentList.count[userLetter]
1

count = sentList.count[userLetter]
2
count = sentList.count[userLetter]
3
count = sentList.count[userLetter]
4
userLetter = input['Enter a letter: ']
5
userLetter = input['Enter a letter: ']
5
count = sentList.count[userLetter]
7

count = sentList.count[userLetter]
8
userLetter = input['Enter a letter: ']
4
userLetter = input['Enter a letter: ']
5
userLetter = input['Enter a letter: ']
4
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.
2
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.
3

userLetter = input['Enter a letter: ']
3
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.
5
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.
6

3 has occurred 2 times
1
3 has occurred 2 times
2
3 has occurred 2 times
3
3 has occurred 2 times
4
3 has occurred 2 times
5
3 has occurred 2 times
6

3 has occurred 2 times
7
3 has occurred 2 times
8

Output:

Các

8 has occurred 5 times 
8
userLetter = input['Enter a letter: ']
5
8 has occurred 5 times
0

8 has occurred 5 times

Python3

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.

count = sentList.count[userLetter]
8
userLetter = input['Enter a letter: ']
4
userLetter = input['Enter a letter: ']
5
userLetter = input['Enter a letter: ']
4
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.
2
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.
3

userLetter = input['Enter a letter: ']
3
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.
5
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.
6

3 has occurred 2 times
1
3 has occurred 2 times
2
3 has occurred 2 times
3
3 has occurred 2 times
4
3 has occurred 2 times
5
3 has occurred 2 times
6

3 has occurred 2 times
7
3 has occurred 2 times
8

Output:

8 has occurred 5 times 

Các

8 has occurred 5 times 
8
userLetter = input['Enter a letter: ']
5
8 has occurred 5 times
0

Python3

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;

userLetter = input['Enter a letter: ']
3
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.
5
2 has occurred 3 times
3

3 has occurred 2 times
1
3 has occurred 2 times
2
3 has occurred 2 times
3
3 has occurred 2 times
4
3 has occurred 2 times
5
userLetter = input['Enter a letter: ']
44

Output:

3 has occurred 2 times

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 []sing countof[]

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

userLetter = input['Enter a letter: ']
08
userLetter = input['Enter a letter: ']
46

userLetter = input['Enter a letter: ']
10
userLetter = input['Enter a letter: ']
5

8 has occurred 5 times 
8
userLetter = input['Enter a letter: ']
5
userLetter = input['Enter a letter: ']
17

3 has occurred 2 times
1
userLetter = input['Enter a letter: ']
76
userLetter = input['Enter a letter: ']
77
userLetter = input['Enter a letter: ']
78

Output:

2 has occurred 3 times

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}

Python3

userLetter = input['Enter a letter: ']
79
userLetter = input['Enter a letter: ']
5
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.
9
userLetter = input['Enter a letter: ']
82
8 has occurred 5 times
1
userLetter = input['Enter a letter: ']
84
8 has occurred 5 times
1__

count = sentList.count[userLetter]
04
userLetter = input['Enter a letter: ']
5
count = sentList.count[userLetter]
06
userLetter = input['Enter a letter: ']
8
count = sentList.count[userLetter]
08__
count = sentList.count[userLetter]
10

3 has occurred 2 times
1
count = sentList.count[userLetter]
12
count = sentList.count[userLetter]
02
count = sentList.count[userLetter]
14

Output:

________số 8

Phương pháp 6: đếm sự xuất hiện của một phần tử trong danh sách sử dụng thư viện Panda

Chức năng pandas sê -ri.value_counts [] Trả về một chuỗi chứa số lượng các giá trị duy nhất. Đối tượng kết quả sẽ theo thứ tự giảm dần để phần tử đầu tiên là phần tử thường xuyên nhất.

Python3

userLetter = input['Enter a letter: ']
08
count = sentList.count[userLetter]
16

userLetter = input['Enter a letter: ']
10
userLetter = input['Enter a letter: ']
5

8 has occurred 5 times 
8
userLetter = input['Enter a letter: ']
5
userLetter = input['Enter a letter: ']
17

3 has occurred 2 times
1
3 has occurred 2 times
2
count = sentList.count[userLetter]
47
userLetter = input['Enter a letter: ']
78

3 has occurred 2 times
1
count = sentList.count[userLetter]
50

Output:

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

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

Python3

userLetter = input['Enter a letter: ']
10
userLetter = input['Enter a letter: ']
5

count = sentList.count[userLetter]
76
userLetter = input['Enter a letter: ']
5
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.
3

8 has occurred 5 times 
8
userLetter = input['Enter a letter: ']
5
userLetter = input['Enter a letter: ']
17

3 has occurred 2 times
1
3 has occurred 2 times
2
count = sentList.count[userLetter]
93
count = sentList.count[userLetter]
94
count = sentList.count[userLetter]
95
count = sentList.count[userLetter]
96
count = sentList.count[userLetter]
97
count = sentList.count[userLetter]
98
count = sentList.count[userLetter]
99
userLetter = input['Enter a letter: ']
78

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

userLetter = input['Enter a letter: ']
0


Làm thế nào để bạn đếm sự lặp lại trong một danh sách trong Python?

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ị. countOf[] is used for counting the number of occurrences of b in a. It counts the number of occurrences of value. It returns the Count of a number of occurrences of value.

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.

Bài Viết Liên Quan

Chủ Đề