Python TQC考題908 單字次數計算,with open() as file: for line in file: for w in line.split()

加入好友
加入社群
Python TQC考題908 單字次數計算,with open() as file: for line in file: for w in line.split() - 儲蓄保險王

fname=input()
n=int(input()) 

“””

#記得要用int or eval數值化

最後一行程式碼

    if cntwords[w] ==n : print(w)

才有辦法判斷

“””

cntwords={}
with open(fname,”r”,encoding=”utf-8″) as file:
for line in file:  #逐行讀入
for w in line.split():  #逐字讀入

“””

#把一行字,依據空格,切割為好幾個字
#一直input進來,做下面兩行程式

for w in line.split()
非w=line.split(),把w做成一個list

也非w in line

“””

if w in cntwords:cntwords[w]+=1
else :cntwords[w] =1

“””

只判斷key是否在字典中

無法判斷value是否在字典中

Python TQC考題908 單字次數計算,with open() as file: for line in file: for w in line.split() - 儲蓄保險王

若有需要的話,使用:

dict.keys()

dict.values()

dict.items()

“””

words=sorted(cntwords)
#只會排序key,將key存在words這個list

for w in words:  #逐字讀入
    if cntwords[w] ==n : print(w)

Python TQC考題908 單字次數計算,with open() as file: for line in file: for w in line.split() - 儲蓄保險王 

“””

read.txt針對答案出現的3個字詞

隨便亂做一個,內容如下:

is
programming
a a
b b a
is is
b b b b
programming
programming

# a , is , programming出現3次

這一題是用字詞當key

出現次數當value

“””

Python TQC考題908 單字次數計算,with open() as file: for line in file: for w in line.split() - 儲蓄保險王

 

#再練習一次:

Python TQC考題908 單字次數計算,with open() as file: for line in file: for w in line.split() - 儲蓄保險王

 

“””

read.txt依據題意隨便亂寫:

b b a is programming
a is programming
a b b is programming

#a, is , programing 出現3次

請撰寫一程式,要求使用者輸入檔名 read.txt,
以及檔案中某單字出現的次數。輸
出符合次數的單字,並依單字的第一個字母大小排序。
(單字的判斷以空白隔開即可)
看到題目要求,依單字第一個字母排序
可以想到要建立一個dict()
key是單字,value是出現次數
“””
fname= input() # read.txt
times = eval(input()) #輸入: 3
with open(fname,”r”,encoding=”utf-8″) as file:
#fname是input()進來,不用再” ”
dic={} #word是key,出現次數是value
for line in file: #逐行讀入檔案
#print(line) #檢查用
for word in line.split():
#逐字執行迴圈內指令
#print(word) #檢查用
if word in dic : dic[word] += 1
else : dic[word] = 1
key = sorted(dic)
#print(key) #檢查用
for i in range(len(key)):

#前段用for word in key:  
word=key[i]
if dic[word] == times : print(word)

Python TQC考題908 單字次數計算,with open() as file: for line in file: for w in line.split() - 儲蓄保險王

 

#上圖的最末段可以寫得更簡潔:

Python TQC考題908 單字次數計算,with open() as file: for line in file: for w in line.split() - 儲蓄保險王

“””

TQC給的txt內容如下:

What is Python language
Python is a widely used high level general purpose interpreted dynamic programming language
Its design philosophy emphasizes code readability and its syntax allows programmers to express concepts in fewer lines of code than possible in languages such as C or Java
Python supports multiple programming paradigms including object oriented imperative and functional programming or procedural styles
It features a dynamic type system and automatic memory management and has a large and comprehensive standard library
The best way we learn anything is by practice and exercise questions We have started this section for those beginner to intermediate who are familiar with Python

“””

Python TQC考題908 單字次數計算,with open() as file: for line in file: for w in line.split() - 儲蓄保險王

輸出結果:

Python TQC考題908 單字次數計算,with open() as file: for line in file: for w in line.split() - 儲蓄保險王

 

#老師用很少用的dict.items()

Python TQC考題908 單字次數計算,with open() as file: for line in file: for w in line.split() - 儲蓄保險王

 

加入好友
加入社群
Python TQC考題908 單字次數計算,with open() as file: for line in file: for w in line.split() - 儲蓄保險王

儲蓄保險王

儲蓄險是板主最喜愛的儲蓄工具,最喜愛的投資理財工具則是ETF,最喜愛的省錢工具則是信用卡

You may also like...

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *