flatMap抛出错误-uni code项没有属性flatMap [英] flatMap throws error -unicode item does not have attribute flatMap
本文介绍了flatMap抛出错误-uni code项没有属性flatMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定的输入RDD或形式
1:6 7
2:5
我怎样才能形式的另一个RDD
1 6
1 7
2 5
等..
失败消息UNI code项没有属性flatMap
高清get_str(X,Y):
.. code到flatmap
返回运
文字= sc.textFile(输入)
RES = text.map(拉姆达L:l.split(:))的地图。(拉姆达(X,Y):get_str(X,Y))
解决方案
我不是真的到Python,但它看起来像你试图用 flatMap
在的你的地图
,而是需要用地图
> flatMap 。在Scala中,我会做:
VAL文本= sc.textFile(输入)
VAL解析度= text.map(L => l.split([\\\\小号:] +))
.flatMap(名单=> list.drop(1).MAP(I =>(名单(0),I)))
请注意,我在两个分裂,
和:
来获得值的列表
在Python同样的事情:
高清to_seq(S):
K,VS = s.split(:)
在vs.split V():
产量K,V文本= sc.parallelize([1:6 7,2:5])
RES = text.flatMap(to_seq)
res.take(3)## [('1','6'),('1','7'),('2','5')]
Given an input rdd or form
1: 6 7
2: 5
How can i get another rdd of form
1 6
1 7
2 5
and so on..
fails with message unicode item does not have attribute flatMap
def get_str(x,y):
..code to flatmap
return op
text = sc.textFile(inputs)
res = text.map(lambda l:l.split(":")).map(lambda (x,y):get_str(x,y))
解决方案
I'm not really into Python, but it looks like you're trying to use flatMap
inside your map
, but rather you need to replace your map
with flatMap
. In Scala, I would do:
val text = sc.textFile(inputs)
val res = text.map(l => l.split("[\\s:]+"))
.flatMap(list => list.drop(1).map(i => (list(0), i)))
Note that I split on both " "
and ":"
to get a list of values.
The same thing in Python:
def to_seq(s):
k, vs = s.split(":")
for v in vs.split():
yield k, v
text = sc.parallelize(["1: 6 7", "2: 5"])
res = text.flatMap(to_seq)
res.take(3)
## [('1', '6'), ('1', '7'), ('2', '5')]
这篇关于flatMap抛出错误-uni code项没有属性flatMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文