Xem thảo luận
Cải thiện bài viết
Lưu bài viết
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 : e0
The original string is : GeeksforGeeks The maximum of all characters in GeeksforGeeks is : e1
The original string is : GeeksforGeeks The maximum of all characters in GeeksforGeeks is : e2
=
The original string is : GeeksforGeeks The maximum of all characters in GeeksforGeeks is : e4
The original string is : GeeksforGeeks The maximum of all characters in GeeksforGeeks is : e5
The original string is : GeeksforGeeks The maximum of all characters in GeeksforGeeks is : e6
The original string is : GeeksforGeeks The maximum of all characters in GeeksforGeeks is : e7
The original string is : GeeksforGeeks The maximum of all characters in GeeksforGeeks is : e8
The original string is : GeeksforGeeks The maximum of all characters in GeeksforGeeks is : e9
lebigot@weinberg /tmp % python count.py
['l', 2]
['o', 2]
['he', 2]
['ll', 2]
0lebigot@weinberg /tmp % python count.py
['l', 2]
['o', 2]
['he', 2]
['ll', 2]
1The original string is : GeeksforGeeks The maximum of all characters in GeeksforGeeks is : e9
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 : e6
The original string is : GeeksforGeeks The maximum of all characters in GeeksforGeeks is : e7
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]
3import 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]
4import 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]
5The original string is : GeeksforGeeks The maximum of all characters in GeeksforGeeks is : e05
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]
8import 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]
4import 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
6lebigot@weinberg /tmp % python count.py
['l', 2]
['o', 2]
['he', 2]
['ll', 2]
=
8lebigot@weinberg /tmp % python count.py
['l', 2]
['o', 2]
['he', 2]
['ll', 2]
9lebigot@weinberg /tmp % python count.py
['l', 2]
['o', 2]
['he', 2]
['ll', 2]
=
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 : e0
The original string is : GeeksforGeeks The maximum of all characters in GeeksforGeeks is : e1
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]
4import 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
6lebigot@weinberg /tmp % python count.py
['l', 2]
['o', 2]
['he', 2]
['ll', 2]
=
8lebigot@weinberg /tmp % python count.py
['l', 2]
['o', 2]
['he', 2]
['ll', 2]
9lebigot@weinberg /tmp % python count.py
['l', 2]
['o', 2]
['he', 2]
['ll', 2]
=
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 : e0
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 print
5 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.