Rumah >pembangunan bahagian belakang >Tutorial Python >Kamus Hari, Kekerapan aksara menggunakan gelung bersarang

Kamus Hari, Kekerapan aksara menggunakan gelung bersarang

Susan Sarandon
Susan Sarandonasal
2025-01-02 19:27:101033semak imbas

Day-Dictionary, Frequency of character using nested loops

Kamus-{}

Kamus digunakan untuk menyimpan nilai data dalam pasangan kunci:nilai.
Kamus ialah koleksi yang tersusun, boleh ditukar dan tidak membenarkan pendua.
Dalam kamus setiap elemen boleh diakses dengan kuncinya, bukan melalui pengindeksan.
Jika kamus tidak mengandungi kunci maka output akan menjadi 'KeyError'.

Contoh:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}

student = {"name":"raja", "class":5}

print(thisdict)
print(student)
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
{'name': 'raja', 'class': 5}

1. Mencari kekerapan setiap huruf dalam rentetan

s = 'lakshmipritha'
name = list(s)
j = 0 
while j<len(name):
    key = name[j]
    count = 1
    i = j+1
    if key != '*':
        while i<len(name):
            if key == name[i]:
                name[i] = '*'
                count+=1
            i+=1
        print(key, count)
    j+=1

l 1
a 2
k 1
s 1
h 2
m 1
i 2
p 1
r 1
t 1

2. Surat muncul Hanya Sekali

s = 'lakshmipritha'
name = list(s)
j = 0 
while j<len(name):
    key = name[j]
    count = 1
    i = j+1
    if key != '*':
        while i<len(name):
            if key == name[i]:
                name[i] = '*'
                count+=1
            i+=1
    if count == 1 and key!='*':
        print(key, count)
    j+=1

l 1
k 1
s 1
m 1
p 1
r 1
t 1

3. Surat paling kerap

s = 'lakshmipritha'
name = list(s)
j = 0 
while j<len(name):
    key = name[j]
    count = 1
    i = j+1
    if key != '*':
        while i<len(name):
            if key == name[i]:
                name[i] = '*'
                count+=1
            i+=1
    if count != 1 and key!='*':
        print(key, count)
    j+=1
a 2
h 2
i 2

4. Huruf Pertama Tidak berulang

s = 'lakshmipritha'
name = list(s)
j = 0 
while j<len(name):
    key = name[j]
    count = 1
    i = j+1
    if key != '*':
        while i<len(name):
            if key == name[i]:
                name[i] = '*'
                count+=1
            i+=1
    if count == 1 and key!='*':
        print(key, count)
        break
    j+=1
l 1

5. Huruf pertama yang diulang

s = 'lakshmipritha'
name = list(s)
j = 0 
while j<len(name):
    key = name[j]
    count = 1
    i = j+1
    if key != '*':
        while i<len(name):
            if key == name[i]:
                name[i] = '*'
                count+=1
            i+=1
    if count != 1 and key!='*':
        print(key, count)
        break
    j+=1
a 2

6. Surat terakhir tidak berulang

last = ' '
last_count = 0 
s = 'lakshmipritha'
name = list(s)
j = 0 
while j<len(name):
    key = name[j]
    count = 1
    i = j+1
    if key != '*':
        while i<len(name):
            if key == name[i]:
                name[i] = '*'
                count+=1
            i+=1
    if count == 1 and key!='*':
            last = key
            last_count = count
        #print(key, count)
    j+=1

print(last, last_count)
t 1

7. Surat terakhir yang diulang

last = ' '
last_count = 0 
s = 'lakshmipritha'
name = list(s)
j = 0 
while j<len(name):
    key = name[j]
    count = 1
    i = j+1
    if key != '*':
        while i<len(name):
            if key == name[i]:
                name[i] = '*'
                count+=1
            i+=1
    if count != 1 and key!='*':
            last = key
            last_count = count
        #print(key, count)
    j+=1

print(last, last_count)
i 2

8. Surat paling kerap

s = 'lakshmipritha'
name = list(s)
j = 0 
last = ' '
last_count = 0 

while j<len(name):
    key = name[j]
    count = 1
    i = j+1
    if key != '*':
        while i<len(name):
            if key == name[i]:
                name[i] = '*'
                count+=1
            i+=1
    if count != 1 and key!='*':
        if count>last_count:
            last = key
            last_count = count
    j+=1

print(last, last_count)
a 2

9. Kekerapan Vokal (a,e,i,o,u)

vowels = ['a','e','i','o','u']
last = ' '
last_count = 0 
s = 'lakshmipritha'
name = list(s)
j = 0 
while j<len(name):
    key = name[j]
    if key in vowels:
        count = 1
        i = j+1
        if key != '*':
            while i<len(name):
                if key == name[i]:
                    name[i] = '*'
                    count+=1
                i+=1
        if key!='*':
            print(key, count)
    j+=1

a 2
i 2

Atas ialah kandungan terperinci Kamus Hari, Kekerapan aksara menggunakan gelung bersarang. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn