【发布时间】:2021-11-23 08:55:54
【问题描述】:
我有以下清单:
List(List(("hello", "goodbye", 12), ("hello", "goodbye", 15)), List(("hello", "test", 18), ("hello", "test", 20)), List(("something", "different", 30), ("something", "different", 18)))
我想得到以下结果:
List(("hello", "goodbye", 27), ("hello", "test", 38), ("something", "different", 48))
我尝试使用 .map 和 .groupBy,但我似乎找不到让它工作的方法。
【问题讨论】:
-
您之前似乎已经进行了某种分组?如果您可以向我们展示您所做的工作以及原始数据的外观以及您的 Scala 版本是什么,我们可能会提出更好的解决方案。 - 无论如何,使用您当前的模型,您只需
map外部列表即可将每个内部列表转换为单个元素,例如:outerList.map(_.reduce { case ((a, b, n1), (_, _, n2)) => (a, b, n1 + n2) })