作者azulazure (azul)
看板Programming
标题[问题] python dictionary 问题
时间Mon May 5 02:03:39 2008
抱歉 标题不知道怎麽下比较好
我在做一个word frequency的程式
我已经从一个大语料库建了一个依照word frequency排的dictionary
看起来像这样
freq = {'1000': ['i', 'the', 'you'], '2000': ['above', 'book', 'gun', 'noise'],
'3000': ['flag', 'dial', 'ban']}
根据这个frequency dictionary, 我要分析在一篇文章中的frequency range
比如说有一篇文章分析出来是这样
inputwords = {'the': 35, 'book': 2, 'noise': 4}
也就是说在这篇文章中, 'the' 出现35次 等等
我现在的问题是, 我想要产生这样的output
1000:35
2000:6
也就是把所有在那个range里的字的出现次数全部加起来
我试着写了一个loop
for key in freq:
for item in inputwords:
if item in freq[key]:
我现在的问题是不知道怎麽把数字相加起来
试过的方式都产生error message
不知道有没有高手可以指点迷津
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 69.143.181.102