
TQC+ 程式語言Python 908 單字次數計算
題目說明:
請撰寫一程式,要求使用者輸入檔名read.txt,以及檔案中某單字出現的次數。輸出符合次數的單字,並依單字的第一個字母大小排序。(單字的判斷以空白隔開即可)
範例輸入
read.txt
3
範例輸出
a
is
programming
檔案下載:
read.txt
程式碼:
f_name = input()
n = int(input())
countDict = {}
f_word = []
with open(f_name, 'r') as file:
for line in file:
for w in line.split():
if w in countDict:
countDict[w] += 1
else:
countDict[w] = 1
for word in countDict:
if countDict[word] == n:
f_word.append(word)
f_word.sort()
for ans in f_word:
print(ans)
TQC+ 程式語言Python 第9類:檔案與異常處理
如果覺得文章內容還不錯的話,麻煩請幫我點個讚!感謝
可以多點幾次喔~~
第一次點讚需使用 Google 或 Facebook 帳號註冊

