![](/img/trans.png)
[英]Combining elements on an rdd of tuple5 based on one of the entires in tuple in scala
[英]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.