【问题标题】:What is the int needed for in map(int, icount) in PydoopPydoop 中的 map(int, icount) 需要什么 int
【发布时间】:2023-02-03 13:00:06
【问题描述】:

官方Pydoop tutorial里面有一个字数统计的例子。

我了解它是如何工作的,但我想知道 map(int, icounts)) 的内部工作原理。

我是否正确地理解了 icounts 是 1 的列表? int 从哪里来,为什么要映射?

# Compute the word frequency

import pydoop

def mapper(_, text, writer):
    for word in text.split():
        writer.emit(word, "1")

def reducer(word, icounts, writer):
    writer.emit(word, sum(map(int, icounts)))

【问题讨论】:

    标签: python hadoop mapreduce


    【解决方案1】:

    int 之类的类型可以作为函数应用于值,以将该值转换为整数(如果它支持该转换)。

    例如:

    s = '1'
    i = int(s)  # `i` will be `1`, where `s` is `'1'`
    

    icounts 在您的示例中可能是一个可迭代的字符串值(如列表),并且将 int 映射到它上面会将其转换为可迭代的整数值。

    【讨论】:

      猜你喜欢
      • 2012-12-05
      • 2021-11-18
      • 2019-01-01
      • 1970-01-01
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      • 2015-12-08
      • 2012-05-16
      相关资源
      最近更新 更多