Hướng dẫn write a python program to count the number of characters (character frequency) in a string - viết chương trình python để đếm số lượng ký tự (tần số ký tự) trong một chuỗi

Cập nhật lần cuối vào ngày 19 tháng 8 năm 2022 21:50:47 (UTC/GMT +8 giờ)

Chuỗi Python: Bài tập-2 với giải pháp

Viết một chương trình Python để đếm số lượng ký tự (tần số ký tự) trong một chuỗi.

Hướng dẫn write a python program to count the number of characters (character frequency) in a string - viết chương trình python để đếm số lượng ký tự (tần số ký tự) trong một chuỗi

Giải pháp mẫu:-:-

Mã Python:

def char_frequency(str1):
    dict = {}
    for n in str1:
        keys = dict.keys()
        if n in keys:
            dict[n] += 1
        else:
            dict[n] = 1
    return dict
print(char_frequency('google.com'))

Đầu ra mẫu:

{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}

Flowchart:

Hướng dẫn write a python program to count the number of characters (character frequency) in a string - viết chương trình python để đếm số lượng ký tự (tần số ký tự) trong một chuỗi

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:

Trình chỉnh sửa mã Python:

Có một cách khác để giải quyết giải pháp này? Đóng góp mã của bạn (và nhận xét) thông qua Disqus.

Trước đây: Viết chương trình Python để tính độ dài của chuỗi. Nếu độ dài chuỗi nhỏ hơn 2, hãy quay lại thay vì chuỗi trống. Write a Python program to calculate the length of a string.
Next: Write a Python program to get a string made of the first 2 and the last 2 chars from a given a string. If the string length is less than 2, return instead of the empty string.

Python: Lời khuyên trong ngày

Làm cho regex có thể đọc được:

import re
from functools import partial

norm_space = partial(res.sub, r'\s+', ' ')
norm_space(' a b\tc d')
'a b c d'

Hướng dẫn write a python program to count the number of characters (character frequency) in a string - viết chương trình python để đếm số lượng ký tự (tần số ký tự) trong một chuỗi
Phương pháp số 2: Sử dụng
Count of all characters in GeeksforGeeks is :
 {'r': 1, 'e': 4, 'k': 2, 'G': 2, 's': 2, 'f': 1, 'o': 1}
4

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.

Count of all characters in GeeksforGeeks is :
 {'r': 1, 'e': 4, 'k': 2, 'G': 2, 's': 2, 'f': 1, 'o': 1}
5
Count of all characters in GeeksforGeeks is :
 {'r': 1, 'e': 4, 'k': 2, 'G': 2, 's': 2, 'f': 1, 'o': 1}
6
Count of all characters in GeeksforGeeks is :
 {'r': 1, 'e': 4, 'k': 2, 'G': 2, 's': 2, 'f': 1, 'o': 1}
7
Count of all characters in GeeksforGeeks is :
 {'r': 1, 'e': 4, 'k': 2, 'G': 2, 's': 2, 'f': 1, 'o': 1}
8
Sample String : google.com'
Expected Result : {'o': 3, 'g': 2, '.': 1, 'e': 1, 'l': 1, 'm': 1, 'c': 1}

Count of all characters in GeeksforGeeks is : Counter({'e': 4, 's': 2, 'k': 2, 'G': 2, 'o': 1, 'r': 1, 'f': 1}) 2{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1} 1 Count of all characters in GeeksforGeeks is : Counter({'e': 4, 's': 2, 'k': 2, 'G': 2, 'o': 1, 'r': 1, 'f': 1}) 4

________ 58 ________ 27 & nbsp;

Count of all characters in GeeksforGeeks is :
 {'r': 1, 'e': 4, 'k': 2, 'G': 2, 's': 2, 'f': 1, 'o': 1}
2
Count of all characters in GeeksforGeeks is :
 {'k': 2, 'e': 4, 's': 2, 'G': 2, 'f': 1, 'r': 1, 'o': 1}
1

def char_frequency(str1):
    dict = {}
    for n in str1:
        keys = dict.keys()
        if n in keys:
            dict[n] += 1
        else:
            dict[n] = 1
    return dict
print(char_frequency('google.com'))

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

Count of all characters in GeeksforGeeks is :
 {'k': 2, 'e': 4, 's': 2, 'G': 2, 'f': 1, 'r': 1, 'o': 1}
2

Phương thức

Count of all characters in GeeksforGeeks is :
 {'k': 2, 'e': 4, 's': 2, 'G': 2, 'f': 1, 'r': 1, 'o': 1}
3 được sử dụng để kiểm tra ký tự xảy ra trước đó trong chuỗi, nếu nó mới, nó gán 0 là ban đầu và nối 1 cho nó, khác, thêm 1 để giữ giá trị trước đó của phần tử đó trong từ điển.

Đăng mã của bạn thông qua Disqus

Đưa ra một chuỗi, tác vụ là tìm tần số của tất cả các ký tự trong chuỗi đó và trả về một từ điển với key as ký tự và value as tần số của nó trong chuỗi đã cho.

Phương pháp số 1: Phương pháp ngây thơ Naive method

Chỉ đơn giản là lặp qua chuỗi và tạo thành một 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, hãy tăng giá trị của nó lên 1.

{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
0____11
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
2

{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
3
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
1
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
5

{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
6
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
7
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
8
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
9

import re
from functools import partial

norm_space = partial(res.sub, r'\s+', ' ')
norm_space(' a b\tc d')
'a b c d'
0____21
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
7
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
8
import re
from functools import partial

norm_space = partial(res.sub, r'\s+', ' ')
norm_space(' a b\tc d')
'a b c d'
4

import re
from functools import partial

norm_space = partial(res.sub, r'\s+', ' ')
norm_space(' a b\tc d')
'a b c d'
5
import re
from functools import partial

norm_space = partial(res.sub, r'\s+', ' ')
norm_space(' a b\tc d')
'a b c d'
6
import re
from functools import partial

norm_space = partial(res.sub, r'\s+', ' ')
norm_space(' a b\tc d')
'a b c d'
7____11
import re
from functools import partial

norm_space = partial(res.sub, r'\s+', ' ')
norm_space(' a b\tc d')
'a b c d'
9

import re
from functools import partial

norm_space = partial(res.sub, r'\s+', ' ')
norm_space(' a b\tc d')
'a b c d'
0
def char_frequency(str1):
    dict = {}
    for n in str1:
        keys = dict.keys()
        if n in keys:
            dict[n] += 1
        else:
            dict[n] = 1
    return dict
print(char_frequency('google.com'))
1
def char_frequency(str1):
    dict = {}
    for n in str1:
        keys = dict.keys()
        if n in keys:
            dict[n] += 1
        else:
            dict[n] = 1
    return dict
print(char_frequency('google.com'))
2

import re
from functools import partial

norm_space = partial(res.sub, r'\s+', ' ')
norm_space(' a b\tc d')
'a b c d'
5
import re
from functools import partial

norm_space = partial(res.sub, r'\s+', ' ')
norm_space(' a b\tc d')
'a b c d'
6
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
1
import re
from functools import partial

norm_space = partial(res.sub, r'\s+', ' ')
norm_space(' a b\tc d')
'a b c d'
9

def char_frequency(str1):
    dict = {}
    for n in str1:
        keys = dict.keys()
        if n in keys:
            dict[n] += 1
        else:
            dict[n] = 1
    return dict
print(char_frequency('google.com'))
7
def char_frequency(str1):
    dict = {}
    for n in str1:
        keys = dict.keys()
        if n in keys:
            dict[n] += 1
        else:
            dict[n] = 1
    return dict
print(char_frequency('google.com'))
8
def char_frequency(str1):
    dict = {}
    for n in str1:
        keys = dict.keys()
        if n in keys:
            dict[n] += 1
        else:
            dict[n] = 1
    return dict
print(char_frequency('google.com'))
9

________ 40 ________ 27 & nbsp;

Count of all characters in GeeksforGeeks is :
 {'r': 1, 'e': 4, 'k': 2, 'G': 2, 's': 2, 'f': 1, 'o': 1}
2
Count of all characters in GeeksforGeeks is :
 {'r': 1, 'e': 4, 'k': 2, 'G': 2, 's': 2, 'f': 1, 'o': 1}
3

Đầu ra:

Count of all characters in GeeksforGeeks is :
 {'r': 1, 'e': 4, 'k': 2, 'G': 2, 's': 2, 'f': 1, 'o': 1}

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

Count of all characters in GeeksforGeeks is :
 {'r': 1, 'e': 4, 'k': 2, 'G': 2, 's': 2, 'f': 1, 'o': 1}
4 Using
Count of all characters in GeeksforGeeks is :
 {'r': 1, 'e': 4, 'k': 2, 'G': 2, 's': 2, 'f': 1, 'o': 1}
4

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.

Count of all characters in GeeksforGeeks is :
 {'r': 1, 'e': 4, 'k': 2, 'G': 2, 's': 2, 'f': 1, 'o': 1}
5
Count of all characters in GeeksforGeeks is :
 {'r': 1, 'e': 4, 'k': 2, 'G': 2, 's': 2, 'f': 1, 'o': 1}
6
Count of all characters in GeeksforGeeks is :
 {'r': 1, 'e': 4, 'k': 2, 'G': 2, 's': 2, 'f': 1, 'o': 1}
7
Count of all characters in GeeksforGeeks is :
 {'r': 1, 'e': 4, 'k': 2, 'G': 2, 's': 2, 'f': 1, 'o': 1}
8

{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
0____11
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
2

{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
3
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
1
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
5

def char_frequency(str1):
    dict = {}
    for n in str1:
        keys = dict.keys()
        if n in keys:
            dict[n] += 1
        else:
            dict[n] = 1
    return dict
print(char_frequency('google.com'))
7
def char_frequency(str1):
    dict = {}
    for n in str1:
        keys = dict.keys()
        if n in keys:
            dict[n] += 1
        else:
            dict[n] = 1
    return dict
print(char_frequency('google.com'))
8
def char_frequency(str1):
    dict = {}
    for n in str1:
        keys = dict.keys()
        if n in keys:
            dict[n] += 1
        else:
            dict[n] = 1
    return dict
print(char_frequency('google.com'))
9

________ 40 ________ 27 & nbsp;

Count of all characters in GeeksforGeeks is :
 {'r': 1, 'e': 4, 'k': 2, 'G': 2, 's': 2, 'f': 1, 'o': 1}
2
Count of all characters in GeeksforGeeks is :
 {'r': 1, 'e': 4, 'k': 2, 'G': 2, 's': 2, 'f': 1, 'o': 1}
3

Đầu ra:

Count of all characters in GeeksforGeeks is : 
Counter({'e': 4, 's': 2, 'k': 2, 'G': 2, 'o': 1, 'r': 1, 'f': 1})

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

Count of all characters in GeeksforGeeks is :
 {'r': 1, 'e': 4, 'k': 2, 'G': 2, 's': 2, 'f': 1, 'o': 1}
4 Using
Count of all characters in GeeksforGeeks is :
 {'k': 2, 'e': 4, 's': 2, 'G': 2, 'f': 1, 'r': 1, 'o': 1}
2

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.

{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
0____11
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
2

{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
3
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
1
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
5

{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
6
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
7
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
8
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
9

import re
from functools import partial

norm_space = partial(res.sub, r'\s+', ' ')
norm_space(' a b\tc d')
'a b c d'
0____21
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
7
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
8
import re
from functools import partial

norm_space = partial(res.sub, r'\s+', ' ')
norm_space(' a b\tc d')
'a b c d'
4

import re
from functools import partial

norm_space = partial(res.sub, r'\s+', ' ')
norm_space(' a b\tc d')
'a b c d'
5
import re
from functools import partial

norm_space = partial(res.sub, r'\s+', ' ')
norm_space(' a b\tc d')
'a b c d'
6
import re
from functools import partial

norm_space = partial(res.sub, r'\s+', ' ')
norm_space(' a b\tc d')
'a b c d'
7____11
import re
from functools import partial

norm_space = partial(res.sub, r'\s+', ' ')
norm_space(' a b\tc d')
'a b c d'
9

import re
from functools import partial

norm_space = partial(res.sub, r'\s+', ' ')
norm_space(' a b\tc d')
'a b c d'
5
import re
from functools import partial

norm_space = partial(res.sub, r'\s+', ' ')
norm_space(' a b\tc d')
'a b c d'
6
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
1
import re
from functools import partial

norm_space = partial(res.sub, r'\s+', ' ')
norm_space(' a b\tc d')
'a b c d'
9

Đầu ra:

Count of all characters in GeeksforGeeks is :
 {'k': 2, 'e': 4, 's': 2, 'G': 2, 'f': 1, 'r': 1, 'o': 1}

def char_frequency(str1):
    dict = {}
    for n in str1:
        keys = dict.keys()
        if n in keys:
            dict[n] += 1
        else:
            dict[n] = 1
    return dict
print(char_frequency('google.com'))
7
def char_frequency(str1):
    dict = {}
    for n in str1:
        keys = dict.keys()
        if n in keys:
            dict[n] += 1
        else:
            dict[n] = 1
    return dict
print(char_frequency('google.com'))
8
def char_frequency(str1):
    dict = {}
    for n in str1:
        keys = dict.keys()
        if n in keys:
            dict[n] += 1
        else:
            dict[n] = 1
    return dict
print(char_frequency('google.com'))
9
Using key 9

________ 40 ________ 27 & nbsp;

Count of all characters in GeeksforGeeks is :
 {'r': 1, 'e': 4, 'k': 2, 'G': 2, 's': 2, 'f': 1, 'o': 1}
2
Count of all characters in GeeksforGeeks is :
 {'r': 1, 'e': 4, 'k': 2, 'G': 2, 's': 2, 'f': 1, 'o': 1}
3

{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
0____11
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
2

{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
3
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
1
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
5

{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
6
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
7
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
8
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
9

import re
from functools import partial

norm_space = partial(res.sub, r'\s+', ' ')
norm_space(' a b\tc d')
'a b c d'
0____21
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
7
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
8
import re
from functools import partial

norm_space = partial(res.sub, r'\s+', ' ')
norm_space(' a b\tc d')
'a b c d'
4

Đầu ra:

Count of all characters in GeeksforGeeks is :
 {'G': 2, 's': 2, 'k': 2, 'e': 4, 'o': 1, 'r': 1, 'f': 1}