美文网首页
Scala list元素如何两两配对

Scala list元素如何两两配对

作者: Reflection_ | 来源:发表于2018-04-08 04:08 被阅读0次

    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元素如何两两配对

          本文链接:https://www.haomeiwen.com/subject/bsouhftx.html