gpt4 book ai didi

python - 在Python中迭代嵌套字典

转载 作者:行者123 更新时间:2023-11-30 09:38:26 25 4
gpt4 key购买 nike

我有大约 20000 个文本文件,编号为 1.txt、2.txt 等等。

现在,我正在创建一个字典 d,其中包含文件 5.txt、10.txt、15.txt 等的文件路径。

d[value]=filepath

ex:
d[5]=d:/articles/5.txt
d[45]=d:/articles/45.txt

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

vs
mln
money

等等..

现在对于字典“d”中的每个文本文件,我需要记录列表中所有单词的出现频率。

因此,我创建了一个 d2[word][file]=count 形式的嵌套字典(这是正确的方法吗?)

where, d2[vs][5]=number of times "vs" occurs in 5.txt

简而言之,对于每个文件,我都会遍历单词列表并计算其出现次数。

如何创建 d2?

我的错误代码是:

import collections, sys, os, re

sys.stdout=open('3.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'))

# returns the next word in the file
def words_generator(fileobj):
for line in fileobj:
for word in line.split():
yield word

d= collections.defaultdict(list)

#to print the filenames:(creation of d)

with open('topics.txt','r') as f:
for line in f.readlines():
value=(line.split('~')[0])
if int(value)%5==0:
file=folderpaths+value+'.txt'
d[value].append(file)

d2= collections.defaultdict(list)

for file in filepaths:
f = open(file,"r")
words = words_generator(f)
for word in words:
if file in d[file]:
d2[word][file]+= 1
#i have no idea how to go further, beyond this point.

请帮忙!!

最佳答案

类似这样的事情:

import os
from collections import Counter,defaultdict
d2 = defaultdict(dict)
word_list = ['vs', 'mln', 'money']
for fil in d.values():
with open(fil[0]) as f:
path, name = os.path.split(fil[0])
words_c = Counter([word for line in f for word in line.split()])
for word in word_list:
d2[word][name] = words_c[word]

现在访问 d2:

d2['vs']['5.txt']

关于python - 在Python中迭代嵌套字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17448228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com