【发布时间】:2019-01-27 16:49:41
【问题描述】:
我有下面的代码来查找两个单词短语的频率。我需要对三个单词短语做同样的事情。
但是,下面的代码似乎不适用于 3 个单词的短语。
from collections import Counter
import re
sentence = "I love TV show makes me happy, I love also comedy show makes me feel like flying"
words = re.findall(r'\w+', sentence)
two_words = [' '.join(ws) for ws in zip(words, words[1:])]
wordscount = {w:f for w, f in Counter(two_words).most_common() if f > 1}
wordscount
{'show makes': 2, 'makes me': 2, 'I love': 2}
【问题讨论】:
标签: python string python-3.x counter