我怎么能不能索引值列表减少?(How can I cannot index into the val

2019-10-17 06:31发布

我使用的映射器在地图结合通过Python的mrjob模块减少工作。 因为我写发出一个对的mapper_final功能,我相信只有一个键值对被发射到我的减速器。

然而,我的减轻作用是犯错:

  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 ? 应该只有在列表中的一对吧?

Answer 1:

occurrences是不是一个list ,它是一个generator 。 如果你想有一个list ,你需要组装发电机结果到列表中。 就像是:

list_occurrences = [ occ for occ in occurrences ]

要么

list_occurrences = list(occurrences)

yield 'Final Value: ', list_occurrences[0] / 2

或者你可以用出现的第一个值occurrences.next()

yield 'Final Value: ', occurrences.next() / 2


文章来源: How can I cannot index into the values list of reduce?