火花+蟒蛇+过滤问题
问题描述:
感激,如果有人可以在下面的代码片段问题提供一些线索现在火花+蟒蛇+过滤问题
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())
error : vs = list(itertools.islice(iterator, batch))
TypeError:() missing 1 required positional argument: 'b'
答
无法解压缩在lambda
功能的元组,lambda a, b:
意味着一个函数有两个参数,即需要一个元组作为argume不是一个函数NT:
一个简单的解决方法是捕获与一个参数的元素,然后使用索引来访问所述第二元件在所述元组:
wc.filter(lambda t: t[1] > 1).collect()
# [('is', 2), ('the', 2)]
由于用于说明后面以及逻辑。它运行良好。 – Suraj
@Suraj,如果你认为它回答了你的问题,请考虑[接受](http://meta.stackexchange.com/a/5235)答案 – MaxU