Python 中是否有一个内置函数返回“一组三个频繁出现的单词(连续)”。我知道如何以编程方式执行此操作,但我正在寻找内置函数。此外,我将这些词存储在 MySQL 表的 1 个字段的行中,因此我正在 python 或 MySQL 中寻找解决方案。
例如,如果我的数据库包含用户评论作为字段,那么我想检索这些评论中最常出现的 3 个连续词。这样的 3 个连续词的一个例子是“我认为”。 我也知道如何使用 SQL 为 1 个单词执行此操作...但是我已经搜索了以前的帖子,但找不到 3 个连续的单词?
最佳答案
没有您需要的内置函数,但是这个列表推导式应该可以工作并且非常简洁:
l = 'there are no builtins for that'.split()
print [" ".join(l[n:n+3]) for n in xrange(len(l)-2)]
['there are no', 'are no builtins', 'no builtins for', 'builtins for that']
然后,调用最后的结果r
:
import collections
c = collections.Counter()
for item in r:
c[item] += 1
print c
Counter({'there are no': 1, 'are no builtins': 1, 'no builtins for': 1, 'builtins for that': 1})
关于python - Python 或 MySQL 中是否有一个内置函数可以返回一组 3 个经常出现的单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9610903/