Đượ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: ']
2userLetter = input['Enter a letter: ']
3userLetter = input['Enter a letter: ']
4userLetter = input['Enter a letter: ']
5 userLetter = input['Enter a letter: ']
6userLetter = input['Enter a letter: ']
3userLetter = input['Enter a letter: ']
8 userLetter = input['Enter a letter: ']
9count = sentList.count[userLetter]
0 count = sentList.count[userLetter]
1count = sentList.count[userLetter]
2count = sentList.count[userLetter]
3 count = sentList.count[userLetter]
4userLetter = input['Enter a letter: ']
5userLetter = input['Enter a letter: ']
5 count = sentList.count[userLetter]
7count = sentList.count[userLetter]
8userLetter = input['Enter a letter: ']
4userLetter = input['Enter a letter: ']
5 userLetter = input['Enter a letter: ']
4Input: 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: ']
3Input: 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 times1
3 has occurred 2 times2
3 has occurred 2 times3
3 has occurred 2 times4
3 has occurred 2 times5
3 has occurred 2 times6
3 has occurred 2 times7
3 has occurred 2 times8
Output:
Các8 has occurred 5 times
8userLetter = input['Enter a letter: ']
5 8 has occurred 5 times
0
userLetter = input['Enter a letter: ']
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]
8userLetter = input['Enter a letter: ']
4userLetter = input['Enter a letter: ']
5 userLetter = input['Enter a letter: ']
4Input: 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: ']
3Input: 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 times1
3 has occurred 2 times2
3 has occurred 2 times3
3 has occurred 2 times4
3 has occurred 2 times5
3 has occurred 2 times6
3 has occurred 2 times7
3 has occurred 2 times8
Output:
8 has occurred 5 times
Các
8 has occurred 5 times8
userLetter = input['Enter a letter: ']
5 8 has occurred 5 times0
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: ']
3Input: 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 times3
3 has occurred 2 times1
3 has occurred 2 times2
3 has occurred 2 times3
3 has occurred 2 times4
3 has occurred 2 times5
userLetter = input['Enter a letter: ']
44Output:
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: ']
46userLetter = input['Enter a letter: ']
10userLetter = input['Enter a letter: ']
58 has occurred 5 times8
userLetter = input['Enter a letter: ']
5 userLetter = input['Enter a letter: ']
173 has occurred 2 times1
userLetter = input['Enter a letter: ']
76userLetter = input['Enter a letter: ']
77userLetter = input['Enter a letter: ']
78Output:
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: ']
79userLetter = 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: ']
828 has occurred 5 times1
userLetter = input['Enter a letter: ']
848 has occurred 5 times1__
count = sentList.count[userLetter]
04userLetter = input['Enter a letter: ']
5 count = sentList.count[userLetter]
06userLetter = input['Enter a letter: ']
8 count = sentList.count[userLetter]
08__ count = sentList.count[userLetter]
103 has occurred 2 times1
count = sentList.count[userLetter]
12count = sentList.count[userLetter]
02count = sentList.count[userLetter]
14Output:
________số 8Phươ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]
16userLetter = input['Enter a letter: ']
10userLetter = input['Enter a letter: ']
58 has occurred 5 times8
userLetter = input['Enter a letter: ']
5 userLetter = input['Enter a letter: ']
173 has occurred 2 times1
3 has occurred 2 times2
count = sentList.count[userLetter]
47userLetter = input['Enter a letter: ']
783 has occurred 2 times1
count = sentList.count[userLetter]
50Output:
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: ']
10userLetter = input['Enter a letter: ']
5count = sentList.count[userLetter]
76userLetter = input['Enter a letter: ']
5Input: 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 times8
userLetter = input['Enter a letter: ']
5 userLetter = input['Enter a letter: ']
173 has occurred 2 times1
3 has occurred 2 times2
count = sentList.count[userLetter]
93count = sentList.count[userLetter]
94count = sentList.count[userLetter]
95count = sentList.count[userLetter]
96count = sentList.count[userLetter]
97count = sentList.count[userLetter]
98count = sentList.count[userLetter]
99userLetter = input['Enter a letter: ']
78Phươ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