【发布时间】:2021-01-14 07:50:48
【问题描述】:
(代码附在最后)
我无法理解以下关于字数问题的代码。据我了解(如果我错了,请纠正),mapper 会在元组中的一组行上生成输出,
"the", 1
"wheels", 1
"on", 1
"the", 1
"bus", 1
"go", 1
"round", 1
"and", 1
"round", 1
我试图了解 python sum 函数如何处理这些单独的元组,我在列表中看到了 sum 的示例,但在元组上找不到太多。减速器中也不应该有一个循环来确保它对所有单词执行操作吗?
from mrjob.job import MRJob
import re
WORD_REGEX = re.compile(r"\b\w+\b")
class Wordcount(MRJob):
def mapper(self, _, line):
words = WORD_REGEX.findall(line)
for word in words:
yield (word.lower(), 1)
def reducer(self, word, counts):
yield(word, sum(counts))
if __name__ == '__main__':
Wordcount.run()
【问题讨论】:
-
您能否更具体地说明问题是什么?