用書 PYTHON王者歸來 作者洪錦魁
計算字典內的單字數和改成小
#改成小寫
lowersong = SONG.lower()
print(lowersong)
#去掉標點符號
for ch in lowersong: if ch in “.,”:
lowersong =lowersong.replace(ch,””)
print(“去掉標點符號”)
print(lowersong)
#歌曲串列
SONGLIST = lowersong.split()
print(“歌曲串列”)
print(SONGLIST)
#字出現的次數
for word in SONGLIST:
if word in SONGDICT:
SONGDICT[word] +=1
else:
SONGDICT[word] =1
print(“字出現的次數”)
print(SONGDICT)

執行結果

簡化的計算字典中字母出現次數
word = “GOODWORKTONY”
wordcount = {alphabet:word.count(alphabet)for alphabet in word}
print(wordcount)

執行結果

字典內容查詢
Class1 ={“Nini”:”Taiwan”, “Momo”:”Japan”, “Koko”:”Korea”, “Roro”:”Iceland”}
wd =input(“Please input the name:”)
if wd in Class1:
print(wd,”Location:”,Class1[wd])
else:
print(“Error”)

執行結果


文件加密的凱薩密碼
eng =”abcdefghijklmnopqrstuvwxyz”
eng_DICT = {}
front5 = eng[:5] //abcde
end5 = eng[5:] //fghi….z
neweng = end5 + front5
newdict = dict(zip(eng,neweng))
text =input(“please input your password:”)
passwordlist=[]
for i in text:
newpassword = newdict[i]
passwordlist.append(newpassword)
newpasswords =””.join(passwordlist) //串列轉字串
print(newdict)
print(“Yor password:”,text)
print(“Yor new password:”,newpasswords)

執行結果

摩斯電碼翻譯器
morse_code= {“A”:”.-“,”B”:”-…”,”C”:”-.-.”,”D”:”-..”,”E”:”.”,”F”:”..-.”,”G”:”–.”,”H”:”….”,”I”:”..”,”J”:”.—“,”K”:”-.-“,”L”:”.-..”,”M”:”–“,”N”:”-.”,”O”:”—“,”P”:”.–.”,”Q”:”–.-“,”R”: “.-.”,”S”:”…”,”T”:”-“,”U”:”..-“,”V”:”…-“,”W”:”.–“,”X”: “-..-“,”Y”:”-.–“,”Z”:”–..”,”0″:”—–“,”1″:”.—-“,”2″:”..—“,”3”:”…–“,”4”:”….-“,”5″:”…..”,”6″:”-….”,”7″:”–…”,”8″:”—..”,”9″:”—-.”}
Text = input(“Please input password:”)
UPTEXT =Text.upper()
for i in UPTEXT:
print(morse_code[i])

執行結果

PYTHON 反轉字典
morse_code_r = {value:key for key,value in morse_code.items()}
print(morse_code_r)
Text = input(“Please input morse code:”)print(morse_code_r[Text])

執行結果

