我有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']
word in the word_set
作为键,初始值作为0
作为计数李>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
操作?就整个语料库而言,我的内存不足。应该有更好的办法,但我自己找不到
您可以使用
defaultdict
或Counter
fromcollections
使用惰性键。例如:相关问题 更多 >
编程相关推荐