0

I'm trying to understand the official example for mrjob clearly

    def mapper(self, _, line):
        yield "chars", len(line)
        yield "words", len(line.split())
        yield "lines", 1

    def reducer(self, key, values):
        yield key, sum(values)
if __name__ == '__main__':
    MRWordFrequencyCount.run()

I can basically understand the thought of MapReduce.but,how does this sentence(yield "lines", 1) work? What's the meaning of "1" ?

reddevilzs
  • 18
  • 5
  • Have a look at the answers to the following question -> https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do-in-python – Colwin May 24 '17 at 11:04
  • thanks friend,it really helped me understand what is "yield", Is it correct for me to understand in this way: cause field is an generator,it will be called n times, n equals line's number, so let the lines's value =1, then we can count line number through sum(values) – reddevilzs May 24 '17 at 12:08
  • 1
    Your understanding is correct. – Colwin May 24 '17 at 12:10

1 Answers1

1

This code returns a tuple: ("lines", 1). Parentheses can be omitted for tuples in situations like this. Each yield returns some aspect of the input, as well as the aspect's value. The number of lines is 1.

The line could have been written like this:

yield ("lines", 1)
Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56