Hướng dẫn write a python program to count the number occurrence of a specific character in a string - viết chương trình python để đếm số lần xuất hiện của một ký tự cụ thể trong một chuỗi

Ví dụ 1: Sử dụng một vòng lặp cho vòng lặp

count = 0

my_string = "Programiz"
my_char = "r"

for i in my_string:
    if i == my_char:
        count += 1

print[count]

Đầu ra

2

Trong ví dụ trên, chúng tôi đã tìm thấy số lượng

2
9 trong
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
0. Các vòng lặp for-loop trên mỗi ký tự của
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
1 và điều kiện IF kiểm tra xem mỗi ký tự của
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
1 là
2
9. Giá trị của
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
4 tăng nếu có một trận đấu.

Ví dụ 2: Sử dụng Phương thức Đếm []

my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]

Đầu ra

2

Trong ví dụ trên, chúng tôi đã tìm thấy số lượng

2
9 trong
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
0. Các vòng lặp for-loop trên mỗi ký tự của
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
1 và điều kiện IF kiểm tra xem mỗi ký tự của
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
1 là
2
9. Giá trị của
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
4 tăng nếu có một trận đấu.

2
6
2
7
2
3
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
7
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
7
s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
print[s.count["o"]]

1
s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
print[s.count["o"]]

2

s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
print[s.count["o"]]

3
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
9
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
7
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
9
s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
print[s.count["o"]]

7
s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
print[s.count["o"]]

8

s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
print[s.count["o"]]

9
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
0
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
1

________ 52 ________ 47 & nbsp;

Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
4
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
5

Đầu ra:

Phương pháp số 2: Sử dụng

my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
5

Mã Python:

s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
print[s.count["o"]]

Đầu ra mẫu:

Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4

Trực quan hóa thực thi mã Python:

Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực hiện chương trình đã nói:

Sử dụng vòng lặp:

Mã Python:

s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
ctr = 0 
for c in s:
   if c == 'o':
       ctr = ctr + 1
print[ctr]

Đầu ra mẫu:

Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4

Flowchart:


Trực quan hóa thực thi mã Python:

Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực hiện chương trình đã nói:

Sử dụng bộ sưu tập.Count []:

Mã Python:

from collections import Counter
s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
ctr = Counter[s] 
print [str[ctr['o']]]

Đầu ra mẫu:

Trực quan hóa thực thi mã Python:

Trực quan hóa thực thi mã Python:

Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực hiện chương trình đã nói:

Sử dụng các chức năng Lambda:

Mã Python:

2
0

Đầu ra mẫu:

Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4

Trực quan hóa thực thi mã Python:

Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực hiện chương trình đã nói:

Sử dụng các biểu thức thông thường:

Mã Python:

2
2

Đầu ra mẫu:

Trực quan hóa thực thi mã Python:

Trực quan hóa thực thi mã Python:

Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực hiện chương trình đã nói:

Sử dụng các biểu thức thông thường:

Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4

Trình chỉnh sửa mã Python: Write a Python program to test if a certain number is greater than all numbers of a list.
Next: Write a Python program to check if a file path is a file or a directory.

Phương pháp số 4: Sử dụng Lambda +

Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
3 +
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
4

Các hàm Lambda, cùng với

Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
3 và ____996 có thể đạt được nhiệm vụ cụ thể này là đếm tổng số lần xuất hiện của phần tử cụ thể trong một chuỗi. Điều này sử dụng ____97 để tổng hợp tất cả các lần xuất hiện thu được bằng cách sử dụng ____96. Naive method

Các

my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
6
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
7
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
8

________ 52 ________ 47 & nbsp;

Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
4
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
5

& nbsp; Phương pháp #5: Sử dụng

2
25

Biểu thức thường xuyên có thể giúp chúng tôi đạt được nhiều nhiệm vụ mã hóa liên quan đến chuỗi. Họ cũng có thể tạo điều kiện cho chúng tôi đạt được nhiệm vụ tìm kiếm sự xuất hiện của phần tử trong chuỗi.

Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
6
2
27

s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
print[s.count["o"]]

9
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
0
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
1

________ 87 ________ 47 & nbsp;

Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
4
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
0
s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
print[s.count["o"]]

1
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
2

Đầu ra:

2
4

Phương pháp số 4: Sử dụng Lambda +

Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
3 +
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
4
Using
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
5

Sử dụng

my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
5 là phương pháp thông thường nhất trong Python để có được sự xuất hiện của bất kỳ yếu tố nào trong bất kỳ container nào. Điều này dễ dàng để viết mã và ghi nhớ và do đó khá phổ biến.

my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
6
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
7
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
8

s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
ctr = 0 
for c in s:
   if c == 'o':
       ctr = ctr + 1
print[ctr]

1
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
7
s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
ctr = 0 
for c in s:
   if c == 'o':
       ctr = ctr + 1
print[ctr]

3
s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
print[s.count["o"]]

1
s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
ctr = 0 
for c in s:
   if c == 'o':
       ctr = ctr + 1
print[ctr]

5

s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
print[s.count["o"]]

9
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
0
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
1

________ 69 ________ 47 & nbsp;

Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
4
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
2

Đầu ra:

2
5

Phương pháp số 3: Sử dụng

Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
3 Using
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
3

Đây là phương pháp ít được biết đến để có được sự xuất hiện của phần tử trên bất kỳ container nào trong Python. Điều này cũng thực hiện nhiệm vụ tương tự như hai phương thức trên, chỉ là một hàm của một bộ sưu tập thư viện khác nhau.

Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
4
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
5
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
6
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
7

my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
6
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
7
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
8

s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
ctr = 0 
for c in s:
   if c == 'o':
       ctr = ctr + 1
print[ctr]

1
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
7
s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
ctr = 0 
for c in s:
   if c == 'o':
       ctr = ctr + 1
print[ctr]

3
s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
print[s.count["o"]]

1
s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
ctr = 0 
for c in s:
   if c == 'o':
       ctr = ctr + 1
print[ctr]

5

s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
print[s.count["o"]]

9
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
0
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
1

________ 69 ________ 47 & nbsp;

Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
4
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
2

Đầu ra:

2
5

Phương pháp số 3: Sử dụng

Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
3 Using lambda +
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
3 +
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
4

Đây là phương pháp ít được biết đến để có được sự xuất hiện của phần tử trên bất kỳ container nào trong Python. Điều này cũng thực hiện nhiệm vụ tương tự như hai phương thức trên, chỉ là một hàm của một bộ sưu tập thư viện khác nhau.

my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
6
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
7
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
8

Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
4
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
5
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
6
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
7

s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
print[s.count["o"]]

9
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
0
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
1

________ 69 ________ 47 & nbsp;

Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
4
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
2

Đầu ra:

2
5

Phương pháp số 3: Sử dụng

Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
3
Method #5 : Using
2
25

Đây là phương pháp ít được biết đến để có được sự xuất hiện của phần tử trên bất kỳ container nào trong Python. Điều này cũng thực hiện nhiệm vụ tương tự như hai phương thức trên, chỉ là một hàm của một bộ sưu tập thư viện khác nhau.

Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
4
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
5
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
6
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
7

my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
6
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
7
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
8

my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
9
my_string = "Programiz"
my_char = "r"

print[my_string.count[my_char]]
7
from collections import Counter
s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
ctr = Counter[s] 
print [str[ctr['o']]]

3

s = "The quick brown fox jumps over the lazy dog."  
print["Original string:"]
print[s]
print["Number of occurrence of 'o' in the said string:"]
print[s.count["o"]]

9
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
0
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
1

________ 69 ________ 47 & nbsp;

Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
4
Original string:
The quick brown fox jumps over the lazy dog.
Number of occurrence of 'o' in the said string:
4
2

Đầu ra:

2
5

Bài Viết Liên Quan

Chủ Đề