我想在python中合并两个列表,以过滤此获得的列表。
我有以下数据框df:
+---+--------+
|v1 | v2 | v |
+---+--------+
| 2| 4| 24|
| 4| 2| 42|
| 1| 1| 11|
| 1| 3| 13|
| 2| 2| 22|
+---+----+---+
我有两个广播变量(collectAsMap):
{'3': ['4'], '1': ['2', '4', '3'], '2': ['3', '4']}
{'3': ['4'], '5': ['6'], '1': ['2']}
我尝试了以下操作以过滤和合并列表
merge_udf = udf(merge, ArrayType(StringType()))
df = df.distinct().withColumn('MergeList', merge_udf(df.v1, df.v2)
其中:
"""merge two lists in one list"""
def merge2List(listA, listB):
merge = [(itemA+itemB) for itemA in listA for itemB in listB]
return merge
"""merge the entry of two entries of dataframes"""
def merge(x, y):
listA = t1.value.get(x)
if(listA is None):
listA = []
listA.append(x)
listB = t2.value.get(y)
if(listB is None):
listB = []
listB.append(y)
m = merge2List(listA, listB)
return m
获得的结果如下:
+---+---------+------------+
|v1 |v2 | MergeList|
+---+---------+------------+
| 2| 4| [34, 44]|
| 4| 2| [42]|
| 1| 1|[22, 42, 32]|
| 1| 3|[24, 44, 34]|
| 2| 2| [32, 42]|
+---+---------+------------+
我有一个t3 Brodcast变量,其中print(list(t3.value.keys()))
给出['24', '42', '11', '13', '22']
现在,我想过滤出合并列表列中每个列表中的元素。因此,我创建以下函数并更新merge2List函数:
def filterList(v):
vert = list(t3.value.keys())
if(v in vert):
return True
return False
"""merge two lists in one list"""
def merge2List(listA, listB):
merge = [(itemA+itemB) for itemA in listA for itemB in listB]
filteredList = filter(filterList, merge)
return filteredList
引发以下异常:
_pickle.PicklingError: Can't pickle <function filterList at 0x2b2fb1aa6840>: attribute lookup filterList on __main__ failed
有人可以帮助识别我的错误吗?
答案 0 :(得分:1)
由于过滤器的计算延迟,因此泡菜无法读取这些值。因为它们还不存在。它返回一个迭代器。试试:
filtered = filter(m_func, m_list)
pickle.dumps(list(filtered))
答案 1 :(得分:1)
尝试:
load()
答案 2 :(得分:1)
以上两个答案都是正确的。但我设法解决以下问题:
def merge2List(listA, listB):
merge = [(itemA+itemB) for itemA in listA for itemB in listB]
filteredList = filter(lambda x: x in list(t3.value.keys()), merge)
return list(filteredList)