“如何匹配”;";在SCALA中列出每个元素和每个元素?
我试图在Scala中组合“n”个列表,但我在这方面是初学者。我有这个: 例如,如果我有“n”个列表,我希望合并所有第一个元素、所有第二个元素等:“如何匹配”;";在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
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) */