'list' 对象在 pyspark 中没有属性 'map'

zah*_*hra 6 python bigdata apache-spark pyspark

我是 pyspark 的新手。我在pyspark中写了这段代码:

def filterOut2(line):
    return [x for x in line if x != 2]
filtered_lists = data.map(filterOut2)
Run Code Online (Sandbox Code Playgroud)

但我收到此错误:

'list' object has no attribute 'map'
Run Code Online (Sandbox Code Playgroud)

如何map在 PySpark 中专门对我的数据执行操作,以允许我将数据过滤为仅那些条件评估为真的值?

Mik*_*ler 6

map(filterOut2, data) 作品:

>>> data = [[1,2,3,5],[1,2,5,2],[3,5,2,8],[6,3,1,2],[5,3,2,5],[4,1,2,5] ]
... def filterOut2(line):
...     return [x for x in line if x != 2]
... list(map(filterOut2, data))
...
[[1, 3, 5], [1, 5], [3, 5, 8], [6, 3, 1], [5, 3, 5], [4, 1, 5]]
Run Code Online (Sandbox Code Playgroud)

map() 只需要 1 个参数(给定 2 个)

看起来你重新定义了map. 试试__builtin__.map(filterOut2, data)

或者,使用列表理解:

>>> [filterOut2(line) for line in data]
[[1, 3, 5], [1, 5], [3, 5, 8], [6, 3, 1], [5, 3, 5], [4, 1, 5]]
Run Code Online (Sandbox Code Playgroud)