我有一个字符串列表的rdd,如下所示:
(['a','b','c'],['1','2','3','4'],['e','f'],...)
现在我想让列表包含每个内部列表中的所有成对组合,如下所示:
(('a','b'),('a','c'),('b','c'),('1','2'),('1','3'),('1','4'),('2','3'),'('2','4'),('3','4'),('e','f'),...)
怎么做?
答案 0 :(得分:3)
您可以将flatMap
与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))