python - 在 python 中查找嵌套字典中的值之和

标签 python dictionary python-3.x machine-learning python-3.3

我有大约 20000 个文本文件,编号为 5.txt、10.txt 等..

我将这些文件的文件路径存储在我创建的列表“list2”中。

我还有一个文本文件“temp.txt”,其中包含 500 个单词的列表

vs
mln
money

等等..

我将这些单词存储在我创建的另一个列表“列表”中。

现在我创建一个嵌套字典 d2[file][word]=“file”中“word”的频率计数

现在,

我需要为每个文本文件迭代这些单词,

我正在尝试获得以下输出:

filename.txt- sum(d[filename][word]*log(prob))

此处,filename.txt 的格式为 5.txt、10.txt 等等...

“prob”,这是我已经获得的值

我基本上需要找到每个外部键(文件)的内部键(单词)值的总和(即单词的频率)。

说:

d['5.txt']['the']=6

这里“the”是我的话,“5.txt”是文件。现在6是“the”在“5.txt”中出现的次数。

同样:

d['5.txt']['as']=2.

我需要找到字典值的总和。

所以,对于 5.txt:我需要我的答案是:

6*log(prob('the'))+2*log(prob('as'))+...`(for all the words in list)

我需要对所有文件执行此操作。

我的问题在于我应该迭代嵌套字典的部分

import collections, sys, os, re

sys.stdout=open('4.txt','w')
from collections import Counter
from glob import glob

folderpath='d:/individual-articles'
folderpaths='d:/individual-articles/'
counter=Counter()
filepaths = glob(os.path.join(folderpath,'*.txt'))


#test contains: d:/individual-articles/5.txt,d:/individual,articles/10.txt,d:/individual-articles/15.txt and so on...
with open('test.txt', 'r') as fi:
    list2= [line.strip() for line in fi]


#temp contains the list of words
with open('temp.txt', 'r') as fi:
    list= [line.strip() for line in fi]


#the dictionary that contains d2[file][word]
d2 =defaultdict(dict)
for fil in list2:
    with open(fil) as f:
       path, name = os.path.split(fil)
       words_c = Counter([word for line in f for word in line.split()])
       for word in list:
           d2[name][word] = words_c[word]



#this portion is also for the generation of dictionary "prob",that is generated from file 2.txt can be overlooked!
with open('2.txt', 'r+') as istream:
for line in istream.readlines():
    try:
        k,r = line.strip().split(':')
        answer_ca[k.strip()].append(r.strip())
    except ValueError:
        print('Ignoring: malformed line: "{}"'.format(line))




#my problem lies here
items = d2.items()
small_d2 = dict(next(items) for _ in range(10))
for fil in list2:
    total=0
    for k,v in small_d2[fil].items():
        total=total+(v*answer_ca[k])
    print("Total of {} is {}".format(fil,total))

最佳答案

for fil in list2:  #list2 contains the filenames
    total = 0
    for k,v in d[fil].iteritems():
        total += v*log(prob[k])  #where prob is a dict

    print "Total of {} is {}".format(fil,total)

关于python - 在 python 中查找嵌套字典中的值之和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17456373/

相关文章:

python - 如何将两行合并为一行?

python - 我如何模拟 PyMongo 以使用 Flask 应用程序进行测试?

python - 如何在 Apple Music 中创建个性化音乐用户 token ?

python - 如何将字典转换为列表列表?

c++ - 如何在 c++11/c++14 中定义一个以枚举为键、以整数为值的映射?

python - 否则缩进错误: unexpected unindent in PyCharm

python - 为什么 find_all 会报错,即使 just find 没有报错? ( python 美汤)

python - 无法检测到在 Ubuntu 上安装包的 Python 路径

python - 有多个值需要解压

python - 如何在 pytest 中为模拟函数提供不同的返回值?