簡體   English   中英

從元組RDD中刪除空字符串

[英]Remove empty strings from a tuple RDD

我有一個格式為(name,[token1, token2, ...])的RDD,其中name是鍵, token是值。 例如: (Robert,['hello', 'movie', '', 'cinema']) ,我想使用map刪除值中的空字符串。

我的嘗試是:

new_tuple = tuple.map(lambda x: (x[0], [s for s in x[1] if len(s)>0]))

獲得(Robert,['hello', 'movie', 'cinema'])

但是我感覺有沒有那么多余的方法呢?

之后,我要刪除在執行上述操作后最終可能沒有任何值(標記)的項目,是否可以進行以下工作?

final_tuple = new_tuple.filter(lambda x: len(x[1])>0)

試試這個: a = (Robert,['hello', 'movie', '', 'cinema'])

然后a = (a[0], list(filter(None, a[1])))

這是從序列中刪除None,False,0,“”,“的最佳方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM