Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“如何匹配”;";在SCALA中列出每个元素和每个元素?_Scala - Fatal编程技术网

“如何匹配”;";在SCALA中列出每个元素和每个元素?

“如何匹配”;";在SCALA中列出每个元素和每个元素?,scala,Scala,我试图在Scala中组合“n”个列表,但我在这方面是初学者。我有这个: 例如,如果我有“n”个列表,我希望合并所有第一个元素、所有第二个元素等: var a = List("book1","book2","book3","book4") var b = List(103, 102, 101, 100) 我想举例说明: 第103册 第2册102 第3册101 第4册100 我用这种方法读取“n”个列表,但我只能显示每个列表,但不能合并 def listar(x: List[colum

我试图在Scala中组合“n”个列表,但我在这方面是初学者。我有这个:

例如,如果我有“n”个列表,我希望合并所有第一个元素、所有第二个元素等:

var a = List("book1","book2","book3","book4")
var b = List(103, 102, 101, 100)
我想举例说明:

第103册
第2册102
第3册101
第4册100

我用这种方法读取“n”个列表,但我只能显示每个列表,但不能合并

   def listar(x: List[column]): List[Any] = {
     if (x.isEmpty) List()
     else print(x.head.values)
     if (x.isEmpty) {
     List()
    } else {
    listar(x.tail)
      }
   }
  // Here I'm sending one list with more list, It's means list(list(),list(),list()...)
  listar(book.columns)    
我怎么做?非常感谢你的帮助

这个想法是这样的:

 var a = List("book1", "book2", "book3", "book4")
 var b = List(103, 102, 101, 100)
 var c = List ("otro1","otro2","otro3","otro4") 
 var d = List (1,2,3,4)

 var e = List(a,b,c,d)
第103册otro1 1
第2册102第2册
如何组合所有第一要素、第二要素等……

  • 您可以使用索引对第一个列表进行迭代
  • 并映射到具有索引的第二个列表
您将获得一个
列表((ElementFromFirstList,ElementFromSecondList))
例如

如果您只想映射firstelem、secondelem的
Map

scala> a.zipWithIndex.map { case (key, index) => (key -> b(index)) }.toMap
res10: scala.collection.immutable.Map[String,Int] = Map(book1 -> 103, book2 -> 102, book3 -> 101, book4 -> 100)
我假设
list1.size==list2.size

  • 您可以使用索引对第一个列表进行迭代
  • 并映射到具有索引的第二个列表
您将获得一个
列表((ElementFromFirstList,ElementFromSecondList))
例如

如果您只想映射firstelem、secondelem的
Map

scala> a.zipWithIndex.map { case (key, index) => (key -> b(index)) }.toMap
res10: scala.collection.immutable.Map[String,Int] = Map(book1 -> 103, book2 -> 102, book3 -> 101, book4 -> 100)
我假设
list1.size==list2.size

使用:

组合
是一个
列表[列表[任何]]

使用:


combined
是一个
列表[List[Any]]

是的,非常感谢。当然,列表的大小总是一样的。但是当我有一张清单的时候,你能告诉我一些想法吗?。是的,我把这个例子放在两个列表中,但是如果我有列表c(列表a(),列表b(),列表d(),列表e()),我如何匹配所有列表的所有元素呢。谢谢你的回答。是的,非常感谢。当然,列表的大小总是一样的。但是当我有一张清单的时候,你能告诉我一些想法吗?。是的,我把这个例子放在两个列表中,但是如果我有列表c(列表a(),列表b(),列表d(),列表e()),我如何匹配所有列表的所有元素呢。谢谢你的回答。
val a = List("book1", "book2", "book3", "book4")
val b = List(103, 102, 101, 100)
val c = List("otro1", "otro2", "otro3", "otro4") 
val d = List(1, 2, 3, 4)

val e = List(a, b, c, d)
val combined = e.transpose
// List(List(book1, 103, otro1, 1), List(book2, 102, otro2, 2), List(book3, 101, otro3, 3), List(book4, 100, otro4, 4))

combined.foreach(println)
/* prints:
List(book1, 103, otro1, 1)
List(book2, 102, otro2, 2)
List(book3, 101, otro3, 3)
List(book4, 100, otro4, 4) */