Scala方法将iterable的每个元素与另一个元素组合在一起?

Geo*_*Geo 9 scala cartesian-product scala-collections

如果我有这个:

val a = Array("a ","b ","c ")
val b = Array("x","y")
Run Code Online (Sandbox Code Playgroud)

我想知道是否存在允许我遍历第一个集合的这种方法,并且对于每个元素,遍历整个第二个集合.例如,如果我们采取的阵列a,我们将有a,x,a,y,b,x,b,y,c,x,c,y.我知道拉链,但从我看到的它只适用于相同尺寸的集合,并且它关联来自相同位置的元素.

小智 24

我不确定"方法",但这只能用嵌套/复合表达for:

val a = Array("a ","b ","c ")
val b = Array("x","y")
for (a_ <- a; b_ <- b) yield (a_, b_)

res0: Array[(java.lang.String, java.lang.String)] = Array((a ,x), (a ,y), (b ,x), (b ,y), (c ,x), (c ,y))
Run Code Online (Sandbox Code Playgroud)

快乐的编码.

  • 而"方法"版本是`a.flatMap(_a => b.map(_b => _a - > _b))` (7认同)

use*_*own 6

对于未知数量的列表,不同长度以及可能不同类型的列表,您可以使用:

def xproduct (xx: List [List[_]]) : List [List[_]] = 
  xx match {
    case aa :: bb :: Nil => 
      aa.map (a => bb.map (b => List (a, b))).flatten       
    case aa :: bb :: cc => 
      xproduct (bb :: cc).map (li => aa.map (a => a :: li)).flatten
    case _ => xx
}
Run Code Online (Sandbox Code Playgroud)

你会打电话给它

xproduct List (List ("a ", "b ", "c "), List ("x", "y"))
Run Code Online (Sandbox Code Playgroud)

但也可以用不同类型的列表来调用它:

scala>  xproduct (List (List ("Beatles", "Stones"), List (8, 9, 10), List ('$', '€')))  
res146: List[List[_]] = List(List(Beatles, 8, $), List(Stones, 8, $), List(Beatles, 8, €), List(Stones, 8, €), List(Beatles, 9, $), List(Stones, 9, $), List(Beatles, 9, €), List(Stones, 9, €), List(Beatles, 10, $), List(Stones, 10, $), List(Beatles, 10, €), List(Stones, 10, €))
Run Code Online (Sandbox Code Playgroud)

如果不能使用列表,则必须将数组转换为列表,并将结果转换回数组.

更新:

在转向惰性集合的过程中,我从索引(从0到组合大小 - 1)到该位置的结果进行了功能映射,可以使用模数和除法进行计算,只需要集中注意力:

def combicount (xx: List [List[_]]): Int = (1 /: xx) (_ * _.length)

def combination (xx: List [List[_]], i: Int): List[_] = xx match {
    case Nil => Nil
    case x :: xs => x(i % x.length) :: combination (xs, i / x.length)
}

def xproduct (xx: List [List[_]]): List [List[_]] = 
  (0 until combicount (xx)).toList.map (i => combination (xx, i))
Run Code Online (Sandbox Code Playgroud)

使用long而不是BigInt是没有问题的.

更新2,迭代器:

class Cartesian (val ll: List[List[_]]) extends Iterator [List[_]] {

  def combicount (): Int = (1 /: ll) (_ * _.length)

  val last = combicount - 1 
  var iter = 0

  override def hasNext (): Boolean = iter < last
  override def next (): List[_] = {
    val res = combination (ll, iter)
    iter += 1
    res
  }

  def combination (xx: List [List[_]], i: Int): List[_] = xx match {
      case Nil => Nil
      case x :: xs => x (i % x.length) :: combination (xs, i / x.length) 
  }
}
Run Code Online (Sandbox Code Playgroud)


zig*_*tar 5

我在我的代码中广泛使用以下内容.请注意,这适用于任意数量的列表.它正在创建一个Iterator而不是一个集合,因此您不必将潜在的巨大结果存储在内存中.

任何改进都非常受欢迎.

/**
  * An iterator, that traverses every combination of objects in a List of Lists.
  * The first Iterable will be incremented fastest. So consider the head as 
  * the "least significant" bit when counting.*/

class CombinationIterator[A](val components: List[Iterable[A]]) extends Iterator[List[A]]{
  private var state: List[BufferedIterator[A]] = components.map(_.iterator.buffered)
  private var depleted = state.exists(_.isEmpty)

  override def next(): List[A] = {
    //this function assumes, that every iterator is non-empty    
    def advance(s: List[(BufferedIterator[A],Iterable[A])]): List[(BufferedIterator[A],A)] = {
      if( s.isEmpty ){
        depleted = true
        Nil
      }
      else {
        assert(!s.head._1.isEmpty)

        //advance and return identity
        val it = s.head._1
        val next = it.next()
        if( it.hasNext){
          //we have simply incremented the head, so copy the rest
          (it,next) :: s.tail.map(t => (t._1,t._1.head))
        } else {
          //we have depleted the head iterator, reset it and increment the rest
          (s.head._2.iterator.buffered,next) :: advance(s.tail)
        }
      }
    }
    //zipping the iterables to the iterators is needed for resseting them
    val (newState, result) = advance(state.zip(components)).unzip

    //update state
    state = newState    

    result
  }

  override def hasNext = !depleted
}
Run Code Online (Sandbox Code Playgroud)

因此,使用这个,您必须编写new CombinationIterator(List(a,b))以获取遍历每个组合的迭代器.

编辑:基于用户未知的版本

请注意,以下版本不是最佳的(性能明智):

  • 索引访问列表(改为使用数组)
  • takeWhile在每个元素之后进行评估

.

scala> def combination(xx: List[List[_]], i: Int): List[_] = xx match {
     | case Nil => Nil
     | case x :: xs => x(i % x.length) :: combination(xs, i/x.length)
     | }
combination: (xx: List[List[_]], i: Int)List[_]

scala> def combinationIterator(ll: List[List[_]]): Iterator[List[_]] = {
     | Iterator.from(0).takeWhile(n => n < ll.map(_.length).product).map(combination(ll,_))
     | }
combinationIterator: (ll: List[List[_]])Iterator[List[_]]

scala> List(List(1,2,3),List("a","b"),List(0.1,0.2,0.3))
res0: List[List[Any]] = List(List(1, 2, 3), List(a, b), List(0.1, 0.2, 0.3))

scala> combinationIterator(res0)
res1: Iterator[List[_]] = non-empty iterator

scala> res1.mkString("\n")
res2: String = 
List(1, a, 0.1)
List(2, a, 0.1)
List(3, a, 0.1)
List(1, b, 0.1)
List(2, b, 0.1)
List(3, b, 0.1)
List(1, a, 0.2)
List(2, a, 0.2)
List(3, a, 0.2)
List(1, b, 0.2)
List(2, b, 0.2)
List(3, b, 0.2)
List(1, a, 0.3)
List(2, a, 0.3)
List(3, a, 0.3)
List(1, b, 0.3)
List(2, b, 0.3)
List(3, b, 0.3)
Run Code Online (Sandbox Code Playgroud)