我有一个这样的数组[(Int,(Int,Int))],
(24, (12, 5)) (12, (17, 3)) (1, (7, 25)) (24, (4, 6)) (1, (13, 3)) (12, (9, 14)) (24, (9, 18))
我想用第一个元素对这些数组进行分组,这意味着我希望:
(24, ((12, 5), (4, 6), (9, 18))) (12, ((17, 3), (9, 14))) (1, ((7, 25), (13, 3)))
谁能帮我这个?
所以你有了:
val arr = Array((24, (12, 5)), (12, (17, 3)), (1, (7, 25)), (24, (4, 6)), (1, (13, 3)), (12, (9, 14)), (24, (9, 18)))
您可以使用组方法进行分组:
val grouped = arr.groupBy(_._1).map {case (k,v) => k -> v.map {case (_, (b,c))=> (b,c)}}
要么:
val grouped = arr.groupBy(_._1).mapValues {_.map {case (_, (b,c))=> (b,c)}}