gpt4 book ai didi

Scala:反转一对多关系

转载 作者:行者123 更新时间:2023-12-04 09:41:37 26 4
gpt4 key购买 nike

<分区>

我有:

val intsPerChar: List[(Char, List[Int])] = List(
'A' -> List(1,2,3),
'B' -> List(2,3)
)

我想获得一个整数与具有映射关系的字符的映射。即,我想得到:

val charsPerInt: Map[Int, List[Char]] = Map(
1 -> List('A'),
2 -> List('A', 'B'),
3 -> List('A', 'B')
)

目前,我正在做以下事情:

val numbers: List[Int] = l.flatMap(_._2).distinct
numbers.map( n =>
n -> l.filter(_._2.contains(n)).map(_._1)
).toMap

是否有一种不太明确的方法来做到这一点?理想情况下是某种 groupBy

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com