Hướng dẫn how do i find the most repeated characters in a string python? - làm cách nào để tìm các ký tự lặp lại nhiều nhất trong chuỗi python?

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận 

    Bài viết này cung cấp cho chúng ta các phương pháp để tìm tần số của ký tự xảy ra tối đa trong chuỗi Python. Đây là tiện ích khá quan trọng hiện nay và kiến ​​thức về nó luôn hữu ích. Hãy để thảo luận về những cách nhất định trong đó nhiệm vụ này có thể được thực hiện. & NBSP;

    Python3

    Phương pháp 1: Phương thức ngây thơ + Max [] & NBSP;

    Trong phương pháp này, chúng tôi chỉ đơn giản lặp qua chuỗi và tạo thành khóa trong từ điển của phần tử mới xảy ra hoặc nếu phần tử đã xảy ra, chúng tôi sẽ tăng giá trị của nó lên 1. Chúng tôi thấy ký tự xảy ra tối đa bằng cách sử dụng tối đa [] trên các giá trị. & NBSP;

    test_str = "GeeksforGeeks"

    print ["The original string is : "

    The original string is : GeeksforGeeks
    The maximum of all characters in GeeksforGeeks is : e
    0
    The original string is : GeeksforGeeks
    The maximum of all characters in GeeksforGeeks is : e
    1

    The original string is : GeeksforGeeks
    The maximum of all characters in GeeksforGeeks is : e
    2=
    The original string is : GeeksforGeeks
    The maximum of all characters in GeeksforGeeks is : e
    4

    The original string is : GeeksforGeeks
    The maximum of all characters in GeeksforGeeks is : e
    5
    The original string is : GeeksforGeeks
    The maximum of all characters in GeeksforGeeks is : e
    6
    The original string is : GeeksforGeeks
    The maximum of all characters in GeeksforGeeks is : e
    7
    The original string is : GeeksforGeeks
    The maximum of all characters in GeeksforGeeks is : e
    8

    The original string is : GeeksforGeeks
    The maximum of all characters in GeeksforGeeks is : e
    9
    lebigot@weinberg /tmp % python count.py
    ['l', 2]
    ['o', 2]
    ['he', 2]
    ['ll', 2]
    
    0
    lebigot@weinberg /tmp % python count.py
    ['l', 2]
    ['o', 2]
    ['he', 2]
    ['ll', 2]
    
    1

    The original string is : GeeksforGeeks
    The maximum of all characters in GeeksforGeeks is : e
    9
    import heapq  # Helps finding the n largest counts
    import collections
    
    def find_max_counts[sequence]:
        """
        Returns an iterator that produces the [element, count]s with the
        highest number of occurrences in the given sequence.
    
        In addition, the elements are sorted.
        """
    
        if len[sequence] == 0:
            raise StopIteration
    
        counter = collections.defaultdict[int]
        for elmt in sequence:
            counter[elmt] += 1
    
        counts_heap = [
            [-count, elmt]  # The largest elmt counts are the smallest elmts
            for [elmt, count] in counter.iteritems[]]
    
        heapq.heapify[counts_heap]
    
        highest_count = counts_heap[0][0]
    
        while True:
    
            try:
                [opp_count, elmt] = heapq.heappop[counts_heap]
            except IndexError:
                raise StopIteration
    
            if opp_count != highest_count:
                raise StopIteration
    
            yield [elmt, -opp_count]
    
    for [letter, count] in find_max_counts['balloon']:
        print [letter, count]
    
    for [word, count] in find_max_counts[['he', 'lkj', 'he', 'll', 'll']]:
        print [word, count]
    
    0
    The original string is : GeeksforGeeks
    The maximum of all characters in GeeksforGeeks is : e
    6
    The original string is : GeeksforGeeks
    The maximum of all characters in GeeksforGeeks is : e
    7
    import heapq  # Helps finding the n largest counts
    import collections
    
    def find_max_counts[sequence]:
        """
        Returns an iterator that produces the [element, count]s with the
        highest number of occurrences in the given sequence.
    
        In addition, the elements are sorted.
        """
    
        if len[sequence] == 0:
            raise StopIteration
    
        counter = collections.defaultdict[int]
        for elmt in sequence:
            counter[elmt] += 1
    
        counts_heap = [
            [-count, elmt]  # The largest elmt counts are the smallest elmts
            for [elmt, count] in counter.iteritems[]]
    
        heapq.heapify[counts_heap]
    
        highest_count = counts_heap[0][0]
    
        while True:
    
            try:
                [opp_count, elmt] = heapq.heappop[counts_heap]
            except IndexError:
                raise StopIteration
    
            if opp_count != highest_count:
                raise StopIteration
    
            yield [elmt, -opp_count]
    
    for [letter, count] in find_max_counts['balloon']:
        print [letter, count]
    
    for [word, count] in find_max_counts[['he', 'lkj', 'he', 'll', 'll']]:
        print [word, count]
    
    3

    import heapq  # Helps finding the n largest counts
    import collections
    
    def find_max_counts[sequence]:
        """
        Returns an iterator that produces the [element, count]s with the
        highest number of occurrences in the given sequence.
    
        In addition, the elements are sorted.
        """
    
        if len[sequence] == 0:
            raise StopIteration
    
        counter = collections.defaultdict[int]
        for elmt in sequence:
            counter[elmt] += 1
    
        counts_heap = [
            [-count, elmt]  # The largest elmt counts are the smallest elmts
            for [elmt, count] in counter.iteritems[]]
    
        heapq.heapify[counts_heap]
    
        highest_count = counts_heap[0][0]
    
        while True:
    
            try:
                [opp_count, elmt] = heapq.heappop[counts_heap]
            except IndexError:
                raise StopIteration
    
            if opp_count != highest_count:
                raise StopIteration
    
            yield [elmt, -opp_count]
    
    for [letter, count] in find_max_counts['balloon']:
        print [letter, count]
    
    for [word, count] in find_max_counts[['he', 'lkj', 'he', 'll', 'll']]:
        print [word, count]
    
    4
    import heapq  # Helps finding the n largest counts
    import collections
    
    def find_max_counts[sequence]:
        """
        Returns an iterator that produces the [element, count]s with the
        highest number of occurrences in the given sequence.
    
        In addition, the elements are sorted.
        """
    
        if len[sequence] == 0:
            raise StopIteration
    
        counter = collections.defaultdict[int]
        for elmt in sequence:
            counter[elmt] += 1
    
        counts_heap = [
            [-count, elmt]  # The largest elmt counts are the smallest elmts
            for [elmt, count] in counter.iteritems[]]
    
        heapq.heapify[counts_heap]
    
        highest_count = counts_heap[0][0]
    
        while True:
    
            try:
                [opp_count, elmt] = heapq.heappop[counts_heap]
            except IndexError:
                raise StopIteration
    
            if opp_count != highest_count:
                raise StopIteration
    
            yield [elmt, -opp_count]
    
    for [letter, count] in find_max_counts['balloon']:
        print [letter, count]
    
    for [word, count] in find_max_counts[['he', 'lkj', 'he', 'll', 'll']]:
        print [word, count]
    
    5
    The original string is : GeeksforGeeks
    The maximum of all characters in GeeksforGeeks is : e
    05
    import heapq  # Helps finding the n largest counts
    import collections
    
    def find_max_counts[sequence]:
        """
        Returns an iterator that produces the [element, count]s with the
        highest number of occurrences in the given sequence.
    
        In addition, the elements are sorted.
        """
    
        if len[sequence] == 0:
            raise StopIteration
    
        counter = collections.defaultdict[int]
        for elmt in sequence:
            counter[elmt] += 1
    
        counts_heap = [
            [-count, elmt]  # The largest elmt counts are the smallest elmts
            for [elmt, count] in counter.iteritems[]]
    
        heapq.heapify[counts_heap]
    
        highest_count = counts_heap[0][0]
    
        while True:
    
            try:
                [opp_count, elmt] = heapq.heappop[counts_heap]
            except IndexError:
                raise StopIteration
    
            if opp_count != highest_count:
                raise StopIteration
    
            yield [elmt, -opp_count]
    
    for [letter, count] in find_max_counts['balloon']:
        print [letter, count]
    
    for [word, count] in find_max_counts[['he', 'lkj', 'he', 'll', 'll']]:
        print [word, count]
    
    8

    import heapq  # Helps finding the n largest counts
    import collections
    
    def find_max_counts[sequence]:
        """
        Returns an iterator that produces the [element, count]s with the
        highest number of occurrences in the given sequence.
    
        In addition, the elements are sorted.
        """
    
        if len[sequence] == 0:
            raise StopIteration
    
        counter = collections.defaultdict[int]
        for elmt in sequence:
            counter[elmt] += 1
    
        counts_heap = [
            [-count, elmt]  # The largest elmt counts are the smallest elmts
            for [elmt, count] in counter.iteritems[]]
    
        heapq.heapify[counts_heap]
    
        highest_count = counts_heap[0][0]
    
        while True:
    
            try:
                [opp_count, elmt] = heapq.heappop[counts_heap]
            except IndexError:
                raise StopIteration
    
            if opp_count != highest_count:
                raise StopIteration
    
            yield [elmt, -opp_count]
    
    for [letter, count] in find_max_counts['balloon']:
        print [letter, count]
    
    for [word, count] in find_max_counts[['he', 'lkj', 'he', 'll', 'll']]:
        print [word, count]
    
    4
    import heapq  # Helps finding the n largest counts
    import collections
    
    def find_max_counts[sequence]:
        """
        Returns an iterator that produces the [element, count]s with the
        highest number of occurrences in the given sequence.
    
        In addition, the elements are sorted.
        """
    
        if len[sequence] == 0:
            raise StopIteration
    
        counter = collections.defaultdict[int]
        for elmt in sequence:
            counter[elmt] += 1
    
        counts_heap = [
            [-count, elmt]  # The largest elmt counts are the smallest elmts
            for [elmt, count] in counter.iteritems[]]
    
        heapq.heapify[counts_heap]
    
        highest_count = counts_heap[0][0]
    
        while True:
    
            try:
                [opp_count, elmt] = heapq.heappop[counts_heap]
            except IndexError:
                raise StopIteration
    
            if opp_count != highest_count:
                raise StopIteration
    
            yield [elmt, -opp_count]
    
    for [letter, count] in find_max_counts['balloon']:
        print [letter, count]
    
    for [word, count] in find_max_counts[['he', 'lkj', 'he', 'll', 'll']]:
        print [word, count]
    
    5=
    import heapq  # Helps finding the n largest counts
    import collections
    
    def find_max_counts[sequence]:
        """
        Returns an iterator that produces the [element, count]s with the
        highest number of occurrences in the given sequence.
    
        In addition, the elements are sorted.
        """
    
        if len[sequence] == 0:
            raise StopIteration
    
        counter = collections.defaultdict[int]
        for elmt in sequence:
            counter[elmt] += 1
    
        counts_heap = [
            [-count, elmt]  # The largest elmt counts are the smallest elmts
            for [elmt, count] in counter.iteritems[]]
    
        heapq.heapify[counts_heap]
    
        highest_count = counts_heap[0][0]
    
        while True:
    
            try:
                [opp_count, elmt] = heapq.heappop[counts_heap]
            except IndexError:
                raise StopIteration
    
            if opp_count != highest_count:
                raise StopIteration
    
            yield [elmt, -opp_count]
    
    for [letter, count] in find_max_counts['balloon']:
        print [letter, count]
    
    for [word, count] in find_max_counts[['he', 'lkj', 'he', 'll', 'll']]:
        print [word, count]
    
    8

    lebigot@weinberg /tmp % python count.py
    ['l', 2]
    ['o', 2]
    ['he', 2]
    ['ll', 2]
    
    6=
    lebigot@weinberg /tmp % python count.py
    ['l', 2]
    ['o', 2]
    ['he', 2]
    ['ll', 2]
    
    8
    lebigot@weinberg /tmp % python count.py
    ['l', 2]
    ['o', 2]
    ['he', 2]
    ['ll', 2]
    
    9= test_str 1

    The original string is : GeeksforGeeks
    The maximum of all characters in GeeksforGeeks is : e

    print [test_str 4

    The original string is : GeeksforGeeks
    The maximum of all characters in GeeksforGeeks is : e
    0 test_str 6test_str 7 

    Đầu ra: & nbsp;

    Python3

    Phương pháp 2: Sử dụng bộ sưu tập.Count [] + Max [] & NBSP;

    Phương pháp được đề xuất nhiều nhất có thể được sử dụng để tìm tất cả các lần xuất hiện là phương pháp này, điều này thực sự có tất cả tần số phần tử và cũng có thể được sử dụng để in tần số phần tử đơn nếu được yêu cầu. Chúng tôi tìm thấy ký tự xảy ra tối đa bằng cách sử dụng tối đa [] trên các giá trị. & Nbsp;

    Trong phương pháp này, chúng tôi chỉ đơn giản lặp qua chuỗi và tạo thành khóa trong từ điển của phần tử mới xảy ra hoặc nếu phần tử đã xảy ra, chúng tôi sẽ tăng giá trị của nó lên 1. Chúng tôi thấy ký tự xảy ra tối đa bằng cách sử dụng tối đa [] trên các giá trị. & NBSP;

    test_str = "GeeksforGeeks"

    print ["The original string is : "

    The original string is : GeeksforGeeks
    The maximum of all characters in GeeksforGeeks is : e
    0
    The original string is : GeeksforGeeks
    The maximum of all characters in GeeksforGeeks is : e
    1

    import heapq  # Helps finding the n largest counts
    import collections
    
    def find_max_counts[sequence]:
        """
        Returns an iterator that produces the [element, count]s with the
        highest number of occurrences in the given sequence.
    
        In addition, the elements are sorted.
        """
    
        if len[sequence] == 0:
            raise StopIteration
    
        counter = collections.defaultdict[int]
        for elmt in sequence:
            counter[elmt] += 1
    
        counts_heap = [
            [-count, elmt]  # The largest elmt counts are the smallest elmts
            for [elmt, count] in counter.iteritems[]]
    
        heapq.heapify[counts_heap]
    
        highest_count = counts_heap[0][0]
    
        while True:
    
            try:
                [opp_count, elmt] = heapq.heappop[counts_heap]
            except IndexError:
                raise StopIteration
    
            if opp_count != highest_count:
                raise StopIteration
    
            yield [elmt, -opp_count]
    
    for [letter, count] in find_max_counts['balloon']:
        print [letter, count]
    
    for [word, count] in find_max_counts[['he', 'lkj', 'he', 'll', 'll']]:
        print [word, count]
    
    4
    import heapq  # Helps finding the n largest counts
    import collections
    
    def find_max_counts[sequence]:
        """
        Returns an iterator that produces the [element, count]s with the
        highest number of occurrences in the given sequence.
    
        In addition, the elements are sorted.
        """
    
        if len[sequence] == 0:
            raise StopIteration
    
        counter = collections.defaultdict[int]
        for elmt in sequence:
            counter[elmt] += 1
    
        counts_heap = [
            [-count, elmt]  # The largest elmt counts are the smallest elmts
            for [elmt, count] in counter.iteritems[]]
    
        heapq.heapify[counts_heap]
    
        highest_count = counts_heap[0][0]
    
        while True:
    
            try:
                [opp_count, elmt] = heapq.heappop[counts_heap]
            except IndexError:
                raise StopIteration
    
            if opp_count != highest_count:
                raise StopIteration
    
            yield [elmt, -opp_count]
    
    for [letter, count] in find_max_counts['balloon']:
        print [letter, count]
    
    for [word, count] in find_max_counts[['he', 'lkj', 'he', 'll', 'll']]:
        print [word, count]
    
    5=
    import heapq  # Helps finding the n largest counts
    import collections
    
    def find_max_counts[sequence]:
        """
        Returns an iterator that produces the [element, count]s with the
        highest number of occurrences in the given sequence.
    
        In addition, the elements are sorted.
        """
    
        if len[sequence] == 0:
            raise StopIteration
    
        counter = collections.defaultdict[int]
        for elmt in sequence:
            counter[elmt] += 1
    
        counts_heap = [
            [-count, elmt]  # The largest elmt counts are the smallest elmts
            for [elmt, count] in counter.iteritems[]]
    
        heapq.heapify[counts_heap]
    
        highest_count = counts_heap[0][0]
    
        while True:
    
            try:
                [opp_count, elmt] = heapq.heappop[counts_heap]
            except IndexError:
                raise StopIteration
    
            if opp_count != highest_count:
                raise StopIteration
    
            yield [elmt, -opp_count]
    
    for [letter, count] in find_max_counts['balloon']:
        print [letter, count]
    
    for [word, count] in find_max_counts[['he', 'lkj', 'he', 'll', 'll']]:
        print [word, count]
    
    8

    lebigot@weinberg /tmp % python count.py
    ['l', 2]
    ['o', 2]
    ['he', 2]
    ['ll', 2]
    
    6=
    lebigot@weinberg /tmp % python count.py
    ['l', 2]
    ['o', 2]
    ['he', 2]
    ['ll', 2]
    
    8
    lebigot@weinberg /tmp % python count.py
    ['l', 2]
    ['o', 2]
    ['he', 2]
    ['ll', 2]
    
    9= test_str 1

    The original string is : GeeksforGeeks
    The maximum of all characters in GeeksforGeeks is : e

    print [test_str 4

    The original string is : GeeksforGeeks
    The maximum of all characters in GeeksforGeeks is : e
    0 test_str 6test_str 7

    Đầu ra: & nbsp;O[n]

    Phương pháp 2: Sử dụng bộ sưu tập.Count [] + Max [] & NBSP;O[n]


    Nếu bạn muốn có tất cả các ký tự với số lượng tối đa, thì bạn có thể thực hiện một biến thể trên một trong hai ý tưởng được đề xuất cho đến nay:

    import heapq  # Helps finding the n largest counts
    import collections
    
    def find_max_counts[sequence]:
        """
        Returns an iterator that produces the [element, count]s with the
        highest number of occurrences in the given sequence.
    
        In addition, the elements are sorted.
        """
    
        if len[sequence] == 0:
            raise StopIteration
    
        counter = collections.defaultdict[int]
        for elmt in sequence:
            counter[elmt] += 1
    
        counts_heap = [
            [-count, elmt]  # The largest elmt counts are the smallest elmts
            for [elmt, count] in counter.iteritems[]]
    
        heapq.heapify[counts_heap]
    
        highest_count = counts_heap[0][0]
    
        while True:
    
            try:
                [opp_count, elmt] = heapq.heappop[counts_heap]
            except IndexError:
                raise StopIteration
    
            if opp_count != highest_count:
                raise StopIteration
    
            yield [elmt, -opp_count]
    
    for [letter, count] in find_max_counts['balloon']:
        print [letter, count]
    
    for [word, count] in find_max_counts[['he', 'lkj', 'he', 'll', 'll']]:
        print [word, count]
    

    Sản lượng này, ví dụ:

    lebigot@weinberg /tmp % python count.py
    ['l', 2]
    ['o', 2]
    ['he', 2]
    ['ll', 2]
    

    Điều này hoạt động với bất kỳ chuỗi nào: từ, nhưng cũng ['xin chào', 'xin chào', 'bonjour'], chẳng hạn.

    Cấu trúc print5 rất hiệu quả trong việc tìm kiếm các yếu tố nhỏ nhất của một chuỗi mà không phân loại hoàn toàn. Mặt khác, vì không có quá nhiều chữ cái trong bảng chữ cái, có lẽ bạn cũng có thể chạy qua danh sách số lượng được sắp xếp cho đến khi số lượng tối đa không được tìm thấy nữa, mà không có bất kỳ tổn thất tốc độ nghiêm trọng nào.

    Làm thế nào để tôi tìm thấy các nhân vật lặp đi lặp lại nhất trong Python?

    Phương pháp 2: Sử dụng Collections.Count [] + Max [] Phương pháp được đề xuất nhiều nhất có thể được sử dụng để tìm tất cả các lần xuất hiện là phương pháp này, điều này thực sự có tất cả tần số phần tử và cũng có thể được sử dụng để in tần số phần tử đơn nếu được yêu cầu. Chúng tôi tìm thấy ký tự xảy ra tối đa bằng cách sử dụng tối đa [] trên các giá trị.Using collections.Counter[] + max[] The most suggested method that could be used to find all occurrences is this method, this actually gets all element frequency and could also be used to print single element frequency if required. We find maximum occurring character by using max[] on values.

    Làm thế nào để bạn tìm thấy ký tự lặp đi lặp lại nhất trong một chuỗi?

    Thực hiện theo các bước để giải quyết vấn đề: Tạo một mảng đếm có kích thước 256 để lưu trữ tần số của mọi ký tự của chuỗi.Duy trì biến tối đa để lưu trữ tần số tối đa cho đến nay bất cứ khi nào gặp tần số nhiều hơn tối đa sau đó cập nhật tối đa.Create a count array of size 256 to store the frequency of every character of the string. Maintain a max variable to store the maximum frequency so far whenever encounter a frequency more than max then update max.

    Làm thế nào để tôi tìm thấy những từ lặp đi lặp lại nhất trong một chuỗi trong Python?

    Cách tiếp cận là đơn giản,..
    Chuỗi phân tách đầu tiên được phân tách bằng không gian ..
    Bây giờ chuyển đổi danh sách các từ thành từ điển bằng cách sử dụng các bộ sưu tập.Phương pháp bộ đếm [iterator].Từ điển chứa các từ là khóa và tần số của nó là giá trị ..
    Bây giờ, danh sách các từ truyền lại và kiểm tra từ đầu tiên có tần số lớn hơn 1 ..

    Làm cách nào để tìm hiểu bao nhiêu lần một chuỗi được lặp lại trong Python?

    Python String Count [] Phương thức đếm [] trả về số lần xuất hiện của một chuỗi con trong chuỗi đã cho. The count[] method returns the number of occurrences of a substring in the given string.

    Bài Viết Liên Quan

    Chủ Đề