问题描述
这是代码。我想将其更改为列表理解,以缩短计算时间。
for sentence in trained['Tweet']:
for uword in unique:
for word in sentence.split(' '):
if uword == word:
count += 1
vector[unique.index(uword)] = count
count = 0
vlist.append(vector)
vector = np.zeros(unique_words)
解决方法
最快的方法是使用计数器:
from collections import Counter
sentence_list = ['Hey there dog','there is a dog','it is a good dog']
word_count = Counter(word for sentence in sentence_list for word in sentence.split())
# Output:
# Counter({'dog': 3,'there': 2,'is': 2,'a': 2,'Hey': 1,'it': 1,'good': 1})