作者harohepowegr (harohepowegr)
看板Python
标题[问题] 读档後存成Dictionary
时间Mon May 12 20:58:13 2014
问题是这样的
老师要求我们要做出读档後
把读出的东西(也就是单字)存成dictionary然後存到另外的file档
并把.txt改成.dic
这边已经写好改档名了
def writeout(file):
inname = file
outname = inname[0:-4]+".dic"
readin = open(file)
writing = open(outname,'w')
想问的是
我的dictionary可以直接写在这个函式里面吗@@?
(基本上不太懂dictionary到底是什麽= =",老师一直强调要把东西写进dictionary
处於鸡同鸭讲的状态QQ
因为我原本的档案有些单字是重复的
但老师只要出现过的而已
所以又有写其他的函式让他记录出现的单字
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 163.14.36.30
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/Python/M.1399899496.A.C6E.html
1F:推 tiefblau:老实说你打完这篇也就google完dict是啥了 他是py的一个资 05/12 21:27
2F:→ tiefblau:料结构 你们老师要的应该是去掉重复的这个特性 因为就跟 05/12 21:27
3F:→ tiefblau:一般用的字典一样 一个key只会有一个值 重复的key会砍掉 05/12 21:28
4F:→ tiefblau:但是用set不就好了吗= =+ 05/12 21:29
5F:→ harohepowegr:那东西写进dictionary可以在上面那个函式接下去写吗? 05/12 21:35
6F:推 funnypeter:可以阿,用pickle 05/12 21:51
7F:→ funnypeter:pickle可以存python的资料结构 05/12 21:52
8F:→ funnypeter:你可以写一个pickle为主的class 05/12 21:52
9F:→ funnypeter:读写操作写在method 05/12 21:53
10F:→ funnypeter:例如 class dic 05/12 21:54
11F:→ funnypeter:a=dic('filename','r') 05/12 21:55
12F:→ funnypeter:然後内部你用pickle打开 05/12 21:55
13F:→ funnypeter:其他读写merhod内部都用pickle帮你完成 05/12 21:57
14F:→ funnypeter:详细自己去官网看说明,1小时就能完成 05/12 21:58
15F:→ mantour:不知道是你没听懂老师的问题 还是老师的问题原本就没说清 05/12 22:22
16F:→ mantour:楚? dict是一个资料结构 而不是一种档案格式 05/12 22:23
17F:→ mantour:dictionary要怎麽写进档案里的方法可以是任意的 05/12 22:28
18F:→ mantour:pickle也不是serialization的唯一方式 05/12 22:28