leonfrank
leonfrank

Reputation: 201

how to create combinations of elements in a list in scala

I have a rdd of list of strings, like the following:

 (['a','b','c'],['1','2','3','4'],['e','f'],...)

now I want to get the list consists of all pairwise combinations in each innerlist, like the following:

 (('a','b'),('a','c'),('b','c'),('1','2'),('1','3'),('1','4'),('2','3'),'('2','4'),('3','4'),('e','f'),...)

How to do that?

Upvotes: 2

Views: 3811

Answers (1)

akuiper
akuiper

Reputation: 214977

You can use flatMap with List.combinations:

val rdd = sc.parallelize(Seq(List("a", "b", "c"), List("1", "2", "3", "4"), List("e", "f")))
// rdd: org.apache.spark.rdd.RDD[List[String]] = ParallelCollectionRDD[0] at parallelize at <console>:24

rdd.flatMap(list => list.combinations(2)).collect()
// res1: Array[List[String]] = Array(List(a, b), List(a, c), List(b, c), List(1, 2), List(1, 3), List(1, 4), List(2, 3), List(2, 4), List(3, 4), List(e, f))

Upvotes: 4

Related Questions