我需要计算句子中的单词数。我用
来做word_matrix[i][j] = sentences[i].count([*words_dict][j])
但是当一个单词包含在另一个单词中时它也很重要,例如“in”包含在“interactive”中。如何避免?
最佳答案
您可以使用collections.Counter
为此:
from collections import Counter
s = 'This is a sentence'
Counter(s.lower().split())
# Counter({'this': 1, 'is': 1, 'a': 1, 'sentence': 1})
关于python - 计算单词和字符串的频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54631463/