Trong bài đăng này, bạn sẽ học cách sử dụng Python để đếm số lần xuất hiện trong một chuỗi. Bạn sẽ học bốn cách khác nhau để thực hiện điều này, bao gồm. phương thức chuỗi >>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2 8 tích hợp sẵn và mô-đun >>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2 9 tuyệt vờiBiết cách làm điều này là một kỹ năng cực kỳ hữu ích, cho phép bạn tìm, chẳng hạn như các giá trị trùng lặp trong một chuỗi hoặc xóa các ký tự không mong muốn (chẳng hạn như các ký tự đặc biệt) Giải pháp dễ dàng. Sử dụng chuỗi >>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2 8>>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print(a_string.count('o'))
4
Mục lục Đếm số lần xuất hiện trong một chuỗi với. đếm()Một trong những cách tích hợp sẵn mà bạn có thể sử dụng Python để đếm số lần xuất hiện trong một chuỗi là sử dụng phương thức tích hợp chuỗi >>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2 8. Phương thức nhận một đối số, ký tự hoặc chuỗi con và trả về số lần ký tự đó tồn tại trong chuỗi được liên kết với phương thứcPhương pháp này rất đơn giản để thực hiện. Trong ví dụ dưới đây, chúng tôi sẽ tải một chuỗi mẫu và sau đó đếm số lần cả ký tự và chuỗi con xuất hiện >>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2
Trong ví dụ trên, bạn đã sử dụng phương thức chuỗi tích hợp sẵn >>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2 8 để đếm số lần cả một ký tự đơn và một chuỗi xuất hiện trong một chuỗi lớn hơnĐếm số lần xuất hiện trong chuỗi Python với bộ đếmĐể tìm một cách linh hoạt và hiệu quả hơn để đếm số lần xuất hiện của một ký tự trong chuỗi Python, bạn cũng có thể sử dụng đối tượng Counter từ mô-đun >>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2 1 tích hợp. Mô-đun này cung cấp một số lớp hữu ích để làm việc với các bộ sưu tập các mục khác nhauTrong trường hợp này, bộ sưu tập của chúng tôi sẽ là một chuỗi. >>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2 2from collections import Counter
a_string = 'the quick brown fox jumps over the lazy dog'
collection = Counter(a_string)
print(collection)
# Returns: Counter({' ': 8, 'o': 4, 'e': 3, 't': 2, 'h': 2, 'u': 2, 'r': 2, 'q': 1, 'i': 1, 'c': 1, 'k': 1, 'b': 1, 'w': 1, 'n': 1, 'f': 1, 'x': 1, 'j': 1, 'm': 1, 'p': 1, 's': 1, 'v': 1, 'l': 1, 'a': 1, 'z': 1, 'y': 1, 'd': 1, 'g': 1})
Những gì chúng tôi đã hoàn thành trong đoạn mã trên là như sau - Chúng tôi đã nhập
>>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2 3 từ mô-đun bộ sưu tập - Sau đó, chúng tôi đã gán chuỗi của mình cho biến
>>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2 4 - Chúng tôi đã chuyển chuỗi vào một đối tượng
>>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2 3 và gọi nó là >>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2 6 - Cuối cùng, chúng tôi đã in đối tượng
>>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2 6 mới
Những gì bạn có thể thấy là những gì được trả về là một đối tượng Counter. Chúng tôi có thể xác nhận điều này bằng cách chạy >>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2 8 trả về >>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2 9Điều tuyệt vời về lớp này là nó chứa một phần tử giống như từ điển chứa các lần xuất hiện của mọi mục có thể lặp lại trong mục đã được chuyển vào Điều này có nghĩa là chúng ta có thể truy cập các lần xuất hiện của các mục khác nhau trong đối tượng của mình bằng cách chuyển vào bộ truy cập từ điển Trong ví dụ bên dưới, hãy xem cách chúng ta có thể thấy tần suất xuất hiện của các chữ cái from collections import Counter
a_string = 'the quick brown fox jumps over the lazy dog'
collection = Counter(a_string)
print(collection)
# Returns: Counter({' ': 8, 'o': 4, 'e': 3, 't': 2, 'h': 2, 'u': 2, 'r': 2, 'q': 1, 'i': 1, 'c': 1, 'k': 1, 'b': 1, 'w': 1, 'n': 1, 'f': 1, 'x': 1, 'j': 1, 'm': 1, 'p': 1, 's': 1, 'v': 1, 'l': 1, 'a': 1, 'z': 1, 'y': 1, 'd': 1, 'g': 1}) 0 và from collections import Counter
a_string = 'the quick brown fox jumps over the lazy dog'
collection = Counter(a_string)
print(collection)
# Returns: Counter({' ': 8, 'o': 4, 'e': 3, 't': 2, 'h': 2, 'u': 2, 'r': 2, 'q': 1, 'i': 1, 'c': 1, 'k': 1, 'b': 1, 'w': 1, 'n': 1, 'f': 1, 'x': 1, 'j': 1, 'm': 1, 'p': 1, 's': 1, 'v': 1, 'l': 1, 'a': 1, 'z': 1, 'y': 1, 'd': 1, 'g': 1}) 1>>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2 4Đây là phép thuật của lớp >>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2 3. nó cho phép bạn dễ dàng truy cập số lần xuất hiện trong Python iterables, chẳng hạn như chuỗiXem một số hướng dẫn Python khác về datagy, bao gồm hướng dẫn đầy đủ của chúng tôi về cách tạo kiểu cho Pandas và tổng quan toàn diện của chúng tôi về Pivot Tables trong Pandas Sử dụng Biểu thức chính quy (Regex) để đếm số lần xuất hiện trong chuỗi PythonBạn cũng có thể sử dụng biểu thức chính quy (regex) để đếm số lần xuất hiện trong chuỗi Python. Cách tiếp cận này hơi quá mức cần thiết, nhưng nếu bạn đã quen thuộc với regex, nó có thể dễ dàng thực hiện Chúng tôi sẽ sử dụng mô-đun biểu thức chính quy, cụ thể là phương thức from collections import Counter
a_string = 'the quick brown fox jumps over the lazy dog'
collection = Counter(a_string)
print(collection)
# Returns: Counter({' ': 8, 'o': 4, 'e': 3, 't': 2, 'h': 2, 'u': 2, 'r': 2, 'q': 1, 'i': 1, 'c': 1, 'k': 1, 'b': 1, 'w': 1, 'n': 1, 'f': 1, 'x': 1, 'j': 1, 'm': 1, 'p': 1, 's': 1, 'v': 1, 'l': 1, 'a': 1, 'z': 1, 'y': 1, 'd': 1, 'g': 1}) 3 để tải các chỉ số về vị trí xuất hiện của ký tự hoặc chuỗi con. Cuối cùng, chúng ta sẽ sử dụng hàm from collections import Counter
a_string = 'the quick brown fox jumps over the lazy dog'
collection = Counter(a_string)
print(collection)
# Returns: Counter({' ': 8, 'o': 4, 'e': 3, 't': 2, 'h': 2, 'u': 2, 'r': 2, 'q': 1, 'i': 1, 'c': 1, 'k': 1, 'b': 1, 'w': 1, 'n': 1, 'f': 1, 'x': 1, 'j': 1, 'm': 1, 'p': 1, 's': 1, 'v': 1, 'l': 1, 'a': 1, 'z': 1, 'y': 1, 'd': 1, 'g': 1}) 4 tích hợp sẵn của Python để xem tần suất xuất hiện của ký tự hoặc chuỗi conHãy xem cách nó hoạt động >>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2 8Chúng ta có thể thấy rằng cách tiếp cận này là một cách làm hơi kỳ quặc, đặc biệt là khi so sánh với hai phương thức ở trên, bao gồm phương thức tích hợp sẵn >>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2 8 và lớp >>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2 3 tích hợp sẵn từ các bộ sưu tậpCuối cùng, hãy xem cách chúng ta có thể đếm số lần xuất hiện bằng cách sử dụng vòng lặp for Sử dụng vòng lặp For để đếm số lần xuất hiện trong chuỗi PythonSử dụng vòng lặp for trong Python để đếm số lần xuất hiện trong chuỗi là một giải pháp hơi ngây thơ, nhưng đôi khi nó có thể hữu ích Cách thức hoạt động của nó là danh sách là các mục mà bạn có thể lặp lại (hoặc thường được gọi là lặp lại), nghĩa là bạn có thể lặp qua từng ký tự trong một chuỗi và đếm xem một ký tự có xuất hiện hay không Hãy triển khai ví dụ bên dưới và sau đó xem cách chúng tôi đã hoàn thành mọi thứ >>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2 0Những gì chúng tôi đã làm ở đây là - Đã khởi tạo một danh sách mới
- Đặt biến
from collections import Counter
a_string = 'the quick brown fox jumps over the lazy dog'
collection = Counter(a_string)
print(collection)
# Returns: Counter({' ': 8, 'o': 4, 'e': 3, 't': 2, 'h': 2, 'u': 2, 'r': 2, 'q': 1, 'i': 1, 'c': 1, 'k': 1, 'b': 1, 'w': 1, 'n': 1, 'f': 1, 'x': 1, 'j': 1, 'm': 1, 'p': 1, 's': 1, 'v': 1, 'l': 1, 'a': 1, 'z': 1, 'y': 1, 'd': 1, 'g': 1}) 7 thành 0 - Lặp lại trên mỗi
from collections import Counter
a_string = 'the quick brown fox jumps over the lazy dog'
collection = Counter(a_string)
print(collection)
# Returns: Counter({' ': 8, 'o': 4, 'e': 3, 't': 2, 'h': 2, 'u': 2, 'r': 2, 'q': 1, 'i': 1, 'c': 1, 'k': 1, 'b': 1, 'w': 1, 'n': 1, 'f': 1, 'x': 1, 'j': 1, 'm': 1, 'p': 1, 's': 1, 'v': 1, 'l': 1, 'a': 1, 'z': 1, 'y': 1, 'd': 1, 'g': 1}) 8 trong chuỗi và đánh giá xem nó có bằng với from collections import Counter
a_string = 'the quick brown fox jumps over the lazy dog'
collection = Counter(a_string)
print(collection)
# Returns: Counter({' ': 8, 'o': 4, 'e': 3, 't': 2, 'h': 2, 'u': 2, 'r': 2, 'q': 1, 'i': 1, 'c': 1, 'k': 1, 'b': 1, 'w': 1, 'n': 1, 'f': 1, 'x': 1, 'j': 1, 'm': 1, 'p': 1, 's': 1, 'v': 1, 'l': 1, 'a': 1, 'z': 1, 'y': 1, 'd': 1, 'g': 1}) 9 không. Nếu đúng như vậy, chúng tôi tăng biến from collections import Counter
a_string = 'the quick brown fox jumps over the lazy dog'
collection = Counter(a_string)
print(collection)
# Returns: Counter({' ': 8, 'o': 4, 'e': 3, 't': 2, 'h': 2, 'u': 2, 'r': 2, 'q': 1, 'i': 1, 'c': 1, 'k': 1, 'b': 1, 'w': 1, 'n': 1, 'f': 1, 'x': 1, 'j': 1, 'm': 1, 'p': 1, 's': 1, 'v': 1, 'l': 1, 'a': 1, 'z': 1, 'y': 1, 'd': 1, 'g': 1}) 7 lên 1. Nếu không, chúng tôi không làm gì cả
Giải pháp này hoạt động, nhưng nó hơi tẻ nhạt khi viết ra và nó không nhanh lắm đối với chuỗi lớn hơn Phần kết luậnTrong bài đăng này, bạn đã học cách sử dụng Python để đếm số lần xuất hiện trong một chuỗi bằng bốn phương thức khác nhau. Cụ thể, bạn đã học cách đếm số lần xuất hiện trong một chuỗi bằng cách sử dụng phương thức >>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2 8 tích hợp, lớp >>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2 3 từ các tập hợp, phương thức from collections import Counter
a_string = 'the quick brown fox jumps over the lazy dog'
collection = Counter(a_string)
print(collection)
# Returns: Counter({' ': 8, 'o': 4, 'e': 3, 't': 2, 'h': 2, 'u': 2, 'r': 2, 'q': 1, 'i': 1, 'c': 1, 'k': 1, 'b': 1, 'w': 1, 'n': 1, 'f': 1, 'x': 1, 'j': 1, 'm': 1, 'p': 1, 's': 1, 'v': 1, 'l': 1, 'a': 1, 'z': 1, 'y': 1, 'd': 1, 'g': 1}) 3 từ biểu thức chính quy của >>> a_string = 'the quick brown fox jumps over the lazy dog'
>>> print('o appears this many times: ', a_string.count('o'))
>>> print('the appears this many times: ', a_string.count('the'))
o appears this many times: 4
ui appears this many times: 2 44, cũng như cách sử dụng vòng lặp for |