如何无法索引到reduce的值列表? [英] How can I cannot index into the values list of reduce?
问题描述
我正在通过Python mrjob模块在Map Reduce作业中使用映射器内合并.因为我写了一个mapper_final函数,该函数会发出一对,所以我确定只有一个键值对会发给我的精简器.
I am using in-mapper combining in a Map Reduce job via the Python mrjob module. Because I wrote a mapper_final function that emits a single pair, I am sure that only a single key-value pair is emitted to my reducers.
但是,我的reduce函数出错了
However, my reduce function is erring:
def reducer(self, key, occurrences):
'''
Calculates the final value.
'''
yield 'Final Value: ', occurrences[0] / 2
读取错误
File "calculateFinalValue.py", line 354, in reducer
yield 'Final Value: ', occurrences[0] / 2
TypeError: 'generator' object has no attribute '__getitem__'
为什么我不能索引到occurrences
?该列表中应该只有一对,对吧?
Why can I not index into occurrences
? There should only be a single pair in that list, right?
推荐答案
occurrences
不是list
,而是 generator
.如果需要list
,则需要将生成器结果组合到一个列表中.像这样:
occurrences
is not a list
, it is a generator
. If you want a list
, you need to assemble the generator results into a list. Something like:
list_occurrences = [ occ for occ in occurrences ]
或
list_occurrences = list(occurrences)
yield 'Final Value: ', list_occurrences[0] / 2
或者您可以通过occurrences.next()
获取出现的第一个值:
Or you can get the first value of occurrences with occurrences.next()
:
yield 'Final Value: ', occurrences.next() / 2
这篇关于如何无法索引到reduce的值列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!