看板 Python 關於我們 聯絡資訊
以下為兩個函式 是要從txt讀檔進來 第一個原不是寫成函式,所以在open的filename打上"檔案名稱.txt"就可以output 第二個是計算每個字出現多少次的函式 def readbook(filename): readin = open(filename) count = [] for line in readin: read = line.split() count = count + read return count def list2dict(count): ldict = dict() for ch in count: ldict[ch]=ldict.get(ch,0)+1 return ldict 想問要如何打print()裡面的內容才可以print出每個字出現幾次 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 163.14.36.3 ※ 文章網址: http://www.ptt.cc/bbs/Python/M.1397736866.A.BD2.html
jlhc:你要print什麼東西和你提供的兩個method的關聯是? 04/17 22:34
Cypresslin:你知道這兩個method怎麼用, 以及他會丟出什麼來嗎? 04/17 23:37
Microscft:在class內的才叫method吧 04/19 02:10
Jason1122:for word in ldict.keys(): 04/19 11:05
Jason1122: print word + ":" + str(ldict[word]) 04/19 11:06
Cypresslin:喔喔對耶 沒注意到XD 04/19 15:47
harohepowegr:不好意思沒說清楚> <這是兩個this is an ant this... 04/24 17:42
harohepowegr:的兩個不同檔案,要計算每個字出現多少次 04/24 17:43
harohepowegr:之後用print(list2dict(readbook('b1.txt'))+(readbo 04/24 17:49
harohepowegr:ok('b2.txt)))就出來了XD 剛學python還不是很熟悉> < 04/24 17:51