作者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/cn.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