我想在python中合并兩個列表過濾這個獲得的列表。我有以下數(shù)據(jù)框 df:+---+--------+|v1 | v2 | v |+---+--------+| 2| 4| 24|| 4| 2| 42|| 1| 1| 11|| 1| 3| 13|| 2| 2| 22|+---+----+---+我有兩個廣播變量(collectAsMap):t1: {'3': ['4'], '1': ['2', '4', '3'], '2': ['3', '4']}t2: {'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得到的結(jié)果如下:+---+---------+------------+|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 廣播變量,其中print(list(t3.value.keys()))給出['24', '42', '11', '13', '22']現(xiàn)在我想過濾掉合并列表列中每個列表中的元素。因此,我創(chuàng)建了以下函數(shù)并更新了 merge2List 函數(shù):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引發(fā)以下異常:_pickle.PicklingError: Can't pickle <function filterList at 0x2b2fb1aa6840>: attribute lookup filterList on __main__ failed有人可以幫助確定我的錯誤在哪里嗎?
3 回答

元芳怎么了
TA貢獻1798條經(jīng)驗 獲得超7個贊
由于過濾器正在懶惰地評估,泡菜無法讀取值。因為它們還不存在。它返回一個迭代器。嘗試:
filtered = filter(m_func, m_list)
pickle.dumps(list(filtered))

慕萊塢森
TA貢獻1810條經(jīng)驗 獲得超4個贊
以上兩個答案都是正確的。但我按照以下方法解決問題:
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)
添加回答
舉報
0/150
提交
取消