【问题标题】:spark + python + filter issue火花+蟒蛇+过滤器问题
【发布时间】:2017-10-28 23:59:33
【问题描述】:

感谢是否有人可以阐明以下代码 sn-p 问题

lineStr= sc.textFile("/input/words.txt")
print (lineStr.collect())
['this file is created to count the no of texts', 'other wise i am just doing fine', 'lets see the output is there']

wc = lineStr.flatMap(lambda l: l.split(" ")).map(lambda x: (x,1)).reduceByKey(lambda w,c: w+c)
print (wc.glom().collect())
[[('this', 1), ('there', 1), ('i', 1), ('texts', 1), ('just', 1), ('fine', 1), ('is', 2), ('other', 1), ('created', 1), ('count', 1), ('of', 1), ('am', 1), ('no', 1), ('output', 1)], [('lets', 1), ('see', 1), ('the', 2), ('file', 1), ('doing', 1), ('wise', 1), ('to', 1)]]

现在,当我尝试使用以下过滤上述数据集以获取大于 1 的计数值时,我收到错误

s = wc.filter(lambda a,b:b>1)
print (s.collect())

错误:vs = list(itertools.islice(iterator, batch))

TypeError: () 缺少 1 个必需的位置参数:'b'

【问题讨论】:

    标签: python apache-spark filter word-count


    【解决方案1】:

    你不能在 lambda 函数中解包元组,lambda a, b: 表示一个接受两个参数的函数,而不是一个接受一个元组作为参数的函数:

    一个简单的解决方法是使用单个参数捕获元素,然后使用索引访问元组中的第二个元素:

    wc.filter(lambda t: t[1] > 1).collect()
    # [('is', 2), ('the', 2)]
    

    【讨论】:

    • 感谢您解释背后的逻辑。效果很好。
    • @Suraj,如果您认为accepting 回答了您的问题,请考虑回答
    猜你喜欢
    • 1970-01-01
    • 2022-06-13
    • 2018-12-17
    • 2018-03-07
    • 1970-01-01
    • 2017-11-05
    • 2012-02-03
    • 1970-01-01
    • 2019-09-17
    相关资源
    最近更新 更多