Scala list元素如何两两配对

stackOverflow Link

val pairs = for(x <- nums; y <- nums) yield (x, y)

For those of you who don't want duplicates:

val uniquePairs = for {
      (x, idxX) <- nums.zipWithIndex
      (y, idxY) <- nums.zipWithIndex
      if idxX < idxY
    } yield (x, y)

val nums = List(1,2,3,4,5)
uniquePairs: List[(Int, Int)] = List((1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), 

你可能感兴趣的:(Scala list元素如何两两配对)