【发布时间】:2022-07-03 07:44:41
【问题描述】:
我正在尝试通过字母 a->z 在文本文件中找到最长的单词。我是 Python 新手,刚刚进入 Mrjob 这是我的代码
from mrjob.job import MRJob
import re
WORD_RE = re.compile(r"[\w']+")
class MRWordFreqCount(MRJob):
def mapper(self, _, line):
for word in WORD_RE.findall(line):
yield word[0].lower(), 1
def combiner(self, word, counts):
yield word, sum(counts)
def reducer(self, _, word_count_pairs):
longest_word = ''
for word in word_count_pairs:
if len(word) > len (longest_word):
longest_word = word
yield max(longest_word)
if __name__ == '__main__':
MRWordFreqCount.run()
输出应该是这样的,但我卡在这里
"r" ["recommendations", "representations"]
"s" ["superciliousness"]
【问题讨论】: