簡體   English   中英

如何通過不在元組中的元素過濾 Scala 中的 RDD 映射

[英]how to filter RDD map in Scala by elements not in tuple

我有一個字數統計的例子。 如果我想過濾掉一個常用詞,我可以這樣做,其中 wordList 是一個元組:

val filterWords = wordList.filter(x => x != "to")

但創建要過濾的單詞列表更有用:

val filterWords = ("a", "to", "the", "of", "I", "you")

你如何在上面的過濾器中使用它? 或者,我該怎么做,這是用 SQL 完成的?

where wordList not in ("a", "to", "the", "of", "I", "you")
val filterWords = Set("a", "to", "the", "of", "I", "you")

wordList.filterNot(filterWords.contains(_))

當且僅當正在考慮的wordList元素在filterWords中時, filterWords.contains才會返回 true。 filterNot將通過contains調用返回 false 的元素。

您創建的是元組而不是列表。

val filterWords = List("a", "to", "the", "of", "I", "you")

然后你可以使用

wordlist.filter(x => filterwords.contains(x))

還可以查看List 的完整 api

暫無
暫無

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

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