首頁 > 後端開發 > Python教學 > Python day-Dictionary,使用嵌套循環的字元頻率

Python day-Dictionary,使用嵌套循環的字元頻率

Susan Sarandon
發布: 2025-01-03 17:54:39
原創
704 人瀏覽過

Python day-Dictionary, Frequency of character using nested loops

字典-{}

-->字典用於以鍵:值對的形式儲存資料值。
-->字典是一個有序的、可更改的、不允許重複的集合。
-->在字典中,每個元素都可以透過它們的鍵來訪問,而不是透過索引。
-->如果字典不包含該鍵,則輸出將為「KeyError」。

範例:

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.找出字串中每個字母的頻率

s = 'guruprasanna'
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
登入後複製

輸出:

g 1
u 2
r 2
p 1
a 3
s 1
n 2
登入後複製

*2。字母只出現一次 *

s = 'guruprasanna'
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
登入後複製

輸出:

g 1
p 1
s 1
登入後複製

3。最常見的字母

s = 'guruprasanna'
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
登入後複製

輸出:

u 2
r 2
a 3
n 2
登入後複製

4。第一個不重複的字母

s = 'guruprasanna'
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
登入後複製

輸出:

g 1
登入後複製

5。第一個重複的字母

s = 'guruprasanna'
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
登入後複製

6。最後一個不重複的字母

last = ' '
last_count = 0 
s = 'guruprasanna'
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)
登入後複製

輸出:

s 1
登入後複製

7。最後重複的字母

last = ' '
last_count = 0 
s = 'guruprasanna'
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)
登入後複製

輸出:

n 2
登入後複製

8。最常見的字母

s = 'guruprasanna'
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)
登入後複製

9。元音出現頻率 (a,e,i,o,u)

vowels = ['a','e','i','o','u']
last = ' '
last_count = 0 
s = 'guruprasanna'
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
登入後複製

輸出:

u 2
a 3
登入後複製

以上是Python day-Dictionary,使用嵌套循環的字元頻率的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板