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

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 key="name[j]" count="1" i="j+1" if while name print j>



<p>輸出:<br>
</p>

<pre class="brush:php;toolbar:false">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 key="name[j]" count="1" i="j+1" if while name and print j>



<p>輸出:<br>
</p>

<pre class="brush:php;toolbar:false">g 1
p 1
s 1

3。最常見的字母

s = 'guruprasanna'
name = list(s)
j = 0 
while j<len key="name[j]" count="1" i="j+1" if while name and print j>



<p>輸出:<br>
</p>

<pre class="brush:php;toolbar:false">u 2
r 2
a 3
n 2

4。第一個不重複的字母

s = 'guruprasanna'
name = list(s)
j = 0 
while j<len key="name[j]" count="1" i="j+1" if while name and print break j>



<p>輸出:<br>
</p>

<pre class="brush:php;toolbar:false">g 1

5。第一個重複的字母

s = 'guruprasanna'
name = list(s)
j = 0 
while j<len key="name[j]" count="1" i="j+1" if while name and print break j>



<p><strong>6。最後一個不重複的字母</strong><br>
</p>

<pre class="brush:php;toolbar:false">last = ' '
last_count = 0 
s = 'guruprasanna'
name = list(s)
j = 0 
while j<len key="name[j]" count="1" i="j+1" if while name and last="key" last_count="count" j print>



<p>輸出:<br>
</p>

<pre class="brush:php;toolbar:false">s 1

7。最後重複的字母

last = ' '
last_count = 0 
s = 'guruprasanna'
name = list(s)
j = 0 
while j<len key="name[j]" count="1" i="j+1" if while name and last="key" last_count="count" j print>



<p>輸出:<br>
</p>

<pre class="brush:php;toolbar:false">n 2

8。最常見的字母

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

while j<len key="name[j]" count="1" i="j+1" if while name and>last_count:
            last = key
            last_count = count
    j+=1

print(last, last_count)
</len>

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 key="name[j]" if in vowels: count="1" i="j+1" while name print j>



<p>輸出:<br>
</p>

<pre class="brush:php;toolbar:false">u 2
a 3

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

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
什麼是Python Switch語句?什麼是Python Switch語句?Apr 30, 2025 pm 02:08 PM

本文討論了版本3.10中介紹的Python的新“匹配”語句,該語句與其他語言相同。它增強了代碼的可讀性,並為傳統的if-elif-el提供了性能優勢

Python中有什麼例外組?Python中有什麼例外組?Apr 30, 2025 pm 02:07 PM

Python 3.11中的異常組允許同時處理多個異常,從而改善了並發方案和復雜操作中的錯誤管理。

Python中的功能註釋是什麼?Python中的功能註釋是什麼?Apr 30, 2025 pm 02:06 PM

Python中的功能註釋將元數據添加到函數中,以進行類型檢查,文檔和IDE支持。它們增強了代碼的可讀性,維護,並且在API開發,數據科學和圖書館創建中至關重要。

Python的單位測試是什麼?Python的單位測試是什麼?Apr 30, 2025 pm 02:05 PM

本文討論了Python中的單位測試,其好處以及如何有效編寫它們。它突出顯示了諸如UNITSEST和PYTEST之類的工具進行測試。

Python中的訪問說明符是什麼?Python中的訪問說明符是什麼?Apr 30, 2025 pm 02:03 PM

文章討論了Python中的訪問說明符,這些說明符使用命名慣例表明班級成員的可見性,而不是嚴格的執法。

Python中的__Init __()是什麼?自我如何在其中發揮作用?Python中的__Init __()是什麼?自我如何在其中發揮作用?Apr 30, 2025 pm 02:02 PM

文章討論了Python的\ _ \ _ Init \ _ \ _()方法和Self在初始化對象屬性中的作用。還涵蓋了其他類方法和繼承對\ _ \ _ Init \ _ \ _()的影響。

python中的@classmethod,@staticmethod和實例方法有什麼區別?python中的@classmethod,@staticmethod和實例方法有什麼區別?Apr 30, 2025 pm 02:01 PM

本文討論了python中@classmethod,@staticmethod和實例方法之間的差異,詳細介紹了它們的屬性,用例和好處。它說明瞭如何根據所需功能選擇正確的方法類型和DA

您如何將元素附加到Python數組?您如何將元素附加到Python數組?Apr 30, 2025 am 12:19 AM

Inpython,YouAppendElementStoAlistusingTheAppend()方法。 1)useappend()forsingleelements:my_list.append(4).2)useextend()orextend()或= formultiplelements:my_list.extend.extend(emote_list)ormy_list = [4,5,6] .3)useInsert()forspefificpositions:my_list.insert(1,5).beaware

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境