2016-12-15 60 views
-3

組合這可以先驗算法斯卡拉星火陣列無需重複

是有用的我有2列:

scala> a.take(3) 
res1: Array[String] = Array(cat, dog, bird) 
scala> b.take(3) 
res2: Array[String] = Array(cat, dog, bird) 

我如何能做到對組合,而無需重複配對? 因此,例如:

與>(cat,dog) 但與重複>(dog, cat)

scala> for (a_ <- a; b_ <-b) yield (a_, b_)  
<console>:35: error: type mismatch; 
found : org.apache.spark.rdd.RDD[(String, String)] 
required: TraversableOnce[?] 

最後,我只想有:

(cat, dog) 
    (cat, bird) 
    (dog, bird) 
+3

請包括代碼爲文字而不是圖像 –

+0

請發表你自己寫的一個解決方案,試圖 – radumanolescu

+1

你可以找到解決方案[這裏](HTTPS一些代碼://i.imgur.com/dsBsmbR.png) – Odomontois

回答

-1

我們使用的過濾器X <ÿ ,那麼我們不需要訂購,因爲這將排除所有不正確的配對。

最後的答案是:

val combinations = a.cartesian(b).filter{case(x,y) => x < y} 
+0

非常確定,數組沒有在標準隱式豐富中定義的'笛卡爾'操作 – Odomontois

+0

@Odomontois,它是一個RDD,儘管來自OP的示例代碼。查看錯誤消息。 –