[問題] 如何print出東西

看板Python作者 (harohepowegr)時間10年前 (2014/04/17 20:14), 編輯推噓1(109)
留言10則, 5人參與, 最新討論串1/1
以下為兩個函式 是要從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

04/17 22:34, , 1F
你要print什麼東西和你提供的兩個method的關聯是?
04/17 22:34, 1F

04/17 23:37, , 2F
你知道這兩個method怎麼用, 以及他會丟出什麼來嗎?
04/17 23:37, 2F

04/19 02:10, , 3F
在class內的才叫method吧
04/19 02:10, 3F

04/19 11:05, , 4F
for word in ldict.keys():
04/19 11:05, 4F

04/19 11:06, , 5F
print word + ":" + str(ldict[word])
04/19 11:06, 5F

04/19 15:47, , 6F
喔喔對耶 沒注意到XD
04/19 15:47, 6F

04/24 17:42, , 7F
不好意思沒說清楚> <這是兩個this is an ant this...
04/24 17:42, 7F

04/24 17:43, , 8F
的兩個不同檔案,要計算每個字出現多少次
04/24 17:43, 8F

04/24 17:49, , 9F
之後用print(list2dict(readbook('b1.txt'))+(readbo
04/24 17:49, 9F

04/24 17:51, , 10F
ok('b2.txt)))就出來了XD 剛學python還不是很熟悉> <
04/24 17:51, 10F
文章代碼(AID): #1JJyMYlI (Python)