【发布时间】:2012-08-02 17:53:05
【问题描述】:
这应该是一个简单的问题,但我就是想不通。我有一本名为TD 的字典。 TD 的 {key1{key2:values}} 是 {1:{u'word':3, u'next':2, u'the':2},2:{...}...},其中 key1 是文档,key2 是文档中的单词,value 是该单词在文档中出现的次数,使用Counter方法获得。
我有大量文档,所以每个文档在 TD 中都有一个条目:
TD = {1:{u'word':2, u'next':1, u'the':5,...},
2:{u'my':4, u'you':1, u'other':2,...},
...
168:{u'word':1, u'person':1, u'and':8,...}}
我现在要做的是检查{1{...}} 中的每个单词,看看它是否出现在其他文档中,并对每个文档重复此过程。对于每个文档中出现一个单词,freq 增加 1。我有一个名为 Score 的新字典,应该如下所示:
{1:{u'word':score, u'next':score,...}, 2:{u'my':score, u'you':score,...}...}
要获得这本词典:
Score={}
count = 0
for x,i in TD[count].iteritems():
freq=1
num=1
for y in TD[num].keys():
if word in TF[num].keys():
freq+=1
num+=1
Score[num]={x:(i*freq)}
num+=1
这给了我以下输出:
{1:{u'word':score}, 2:{u'next':score}, 3:{u'the':score}...}
应该是:
{1:{u'word':score, u'next':score, u'the':score,...}...}
我认为问题出在Score[num]={x:(i*freq)}这一行
【问题讨论】:
标签: python dictionary for-loop