【问题标题】:linking one dictionary to another in python在python中将一个字典链接到另一个字典
【发布时间】:2013-06-22 14:27:25
【问题描述】:

我有一本格式如下的字典:

d[class name]=(list of files)

例如:

d[earn]=(6,7,4)

其中 6.txt、7.txt 和 4.txt 是属于“earn”类的文件

现在,我需要创建另一个字典 d2,这样:

d2[earn]=(12,3,2,17)

在哪里

  • 12是“earn”这个词在6.txt中出现的次数,
  • 3是“earn”这个词在7.txt中出现的次数,
  • 4是“earn”这个词在4.txt中出现的次数
  • 17 是单词“earn”在所有三个文件中出现的次数,即;总和。

这是我的代码:

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


folderpath='d:/individual-articles'
counter=Counter()

with open('topics.txt') as f:
    d= collections.defaultdict(list)
    for line in f:
        value, *keys = line.strip().split('~')
        for key in filter(None, keys):
            d[key].append(value+".txt")

filepaths = glob(os.path.join(folderpath,'*.txt'))

def words_generator(fileobj):
    for line in fileobj:
        for word in line.split():
            yield word
word_count_dict = {}
for file in filepaths:
    f = open(file,"r")
    words = words_generator(f)
    for word in words:
        if word not in word_count_dict:
              word_count_dict[word] = {"total":0}
        if file not in word_count_dict[word]:
              word_count_dict[word][file] = 0
        word_count_dict[word][file] += 1              
        word_count_dict[word]["total"] += 1        
for k in word_count_dict.keys():
    for filename in word_count_dict[k]:
        if filename == 'total': continue
        counter.update(filename)

for word, counts in word_count_dict.items():
    print(word, counts['total'])

我需要打印 d2,但我的代码不起作用。

【问题讨论】:

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


    【解决方案1】:

    我认为这应该可以满足您的需求:

    from collections import defaultdict
    d2 = defaultdict(list)
    for word,files in d.items():
        for fname in files: #go over each file name in the 'list' which was associated with the key 'word' in d
            with open(fname) as f: 
                d2[word].append(f.read().count(word)) #add the count of the word in the file
               #d2[word].append(f.read().split().count(word)) use this if you want words not occurances of 'word' in the file
        d2[word].append(sum(d2[word])) #add the sum of all the counts
    print d2
    

    【讨论】:

    • 请注意,f.read().count(word) 可能会产生意想不到的结果:"foobar".count('foo')-->1,首选regex。
    • @AshwiniChaudhary 是的,我意识到在发布后,在其下方添加了 f.read().split().count(word) 方式。使用正则表达式可能会更好,OP 可以对此进行调查。
    • 我得到这个错误:文件“C:\Python33\access_dict.py”,第 36 行,在 中,open(fname) as f: FileNotFoundError: [Errno 2] No such file or目录:'16951'
    • @radhika 如果完整文件名未包含在文件名列表中,您可能需要转到+'.txt'。
    • @HennyH: 你的意思是 open(fname+'.txt') as f: ??
    猜你喜欢
    • 1970-01-01
    • 2021-02-07
    • 2022-07-10
    • 2020-01-02
    • 2019-05-08
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多