从更大的语料库中创建dict

2024-10-02 00:29:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我有30000条信息

corpus = [
    "hello world", 
    "i like mars", 
    "a planet called venus", 
    ... , 
    "it's all pcj500"]

我已经标记了它们,并形成了一个包含所有唯一单词的word_set

word_lists = [text.split(" ") for text in corpus]
>>> [['hello', 'world'],
    ['i', 'like', 'mars'],
    ['a', 'planet', 'called', 'venus'],
    ...,
    ["it's", 'all', 'pcj500']]

word_set = set().union(*word_lists)
>>> ['hello', 'world', 'i', 'like', ..., 'pcj500']
  1. 我正在尝试创建一个字典列表,其中word in the word_set作为,初始值作为0作为计数
  2. 如果word in word_set出现在word_list in word_lists中,则 适当计数为

对于步骤1,我是这样做的

tmp = corpus[:10]
word_dicts = []
for i in range(len(tmp)):
    word_dicts.append(dict.fromkeys(list(word_set)[:30], 0))

word_dicts
>>> [{'hello': 0,
  'world': 0,
  'mars': 0,
  'venus': 0,
  'explore': 0,
  'space': 0}]

问题:

如何对语料库中的所有文本对word_set中的所有项目执行dict.fromkeys操作?就整个语料库而言,我的内存不足。应该有更好的办法,但我自己找不到


Tags: inhelloworlditcorpuslistslikeword
1条回答
网友
1楼 · 发布于 2024-10-02 00:29:35

您可以使用defaultdictCounterfrom collections使用惰性键。例如:

from collections import Counter

word_dicts = []
for words_list in word_lists:
    word_dicts.append(Counter(words_list))

相关问题 更多 >

    热门问题