[英]How to map the values inside a list with an integer in Scala?
我對 Scala 和函數式編程很新鮮。 我堅持在 Scala 中使用 collections 進行操作。我有一個這樣的變量:
val res4: List[(List[Double], Option[Int])] =
List(
(List(4.0, 2.0, 3.0, 4.0, 3.0, 2.5, 4.0),1998),
(List(3.0, 4.0, 3.0, 3.0, 3.5, 2.0, 3.0, 3.0, 4.0).2000,
.......
)
我想通過使用列表中的每個分數來獲得 map 或類似的東西:
(4.0, 1998),
(2.0, 1998),
(3.0, 1998),
(4.0, 1998),
(3.0, 1998),
(2.5, 1998),
(4.0, 1998),
(3.0, 1998),
....
我怎樣才能做到這一點?
此外,如果您知道如何將 Some(1998) 轉換為 1998 的技巧,我將不勝感激。
你可以使用flatMap :
List(
(List(4.0, 2.0, 3.0, 4.0, 3.0, 2.5, 4.0), Some(1998)),
(List(3.0, 4.0, 3.0, 3.0, 3.5, 2.0, 3.0, 3.0, 4.0), Some(2000))
)
.flatMap(row => row._1.map(number => (number, row._2)))
.foreach(it => println(it))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.