[问题] 如何print出东西

楼主: harohepowegr (harohepowegr)   2014-04-17 20:14:24
以下为两个函式
是要从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出每个字出现几次
作者: jlhc (H)   2014-04-17 22:34:00
你要print什么东西和你提供的两个method的关联是?
作者: Cypresslin (啊哈哈~~)   2014-04-17 23:37:00
你知道这两个method怎么用, 以及他会丢出什么来吗?
作者: Microscft (小软哥)   2014-04-19 02:10:00
在class内的才叫method吧
作者: Jason1122 (Jason1122)   2014-04-19 11:05:00
for word in ldict.keys():print word + ":" + str(ldict[word])
作者: Cypresslin (啊哈哈~~)   2014-04-19 15:47:00
喔喔对耶 没注意到XD
楼主: harohepowegr (harohepowegr)   2014-04-24 17:42:00
不好意思没说清楚> <这是两个this is an ant this...的两个不同档案,要计算每个字出现多少次之后用print(list2dict(readbook('b1.txt'))+(readbook('b2.txt)))就出来了XD 刚学python还不是很熟悉> <

Links booklink

Contact Us: admin [ a t ] ucptt.com