作者harohepowegr (harohepowegr)
看板Python
標題[問題] 如何print出東西
時間Thu Apr 17 20:14:24 2014
以下為兩個函式
是要從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://webptt.com/m.aspx?n=bbs/Python/M.1397736866.A.BD2.html
1F:推 jlhc:你要print什麼東西和你提供的兩個method的關聯是? 04/17 22:34
2F:→ Cypresslin:你知道這兩個method怎麼用, 以及他會丟出什麼來嗎? 04/17 23:37
3F:→ Microscft:在class內的才叫method吧 04/19 02:10
4F:→ Jason1122:for word in ldict.keys(): 04/19 11:05
5F:→ Jason1122: print word + ":" + str(ldict[word]) 04/19 11:06
6F:→ Cypresslin:喔喔對耶 沒注意到XD 04/19 15:47
7F:→ harohepowegr:不好意思沒說清楚> <這是兩個this is an ant this... 04/24 17:42
8F:→ harohepowegr:的兩個不同檔案,要計算每個字出現多少次 04/24 17:43
9F:→ harohepowegr:之後用print(list2dict(readbook('b1.txt'))+(readbo 04/24 17:49
10F:→ harohepowegr:ok('b2.txt)))就出來了XD 剛學python還不是很熟悉> < 04/24 17:51