繁体   English   中英

元组列表中每个第一个元素的scala获取第二个元素的最大值

[英]scala for every first element in a list of tuples get max of the second element

我有一个如下的元组列表

List((a,1), (a,2), (b,2), (b,1)) 

我想从上面的列表中创建一个像下面的列表

List((a,2), (b,2))

仅一个第一元素和第二元素的MAX。 我该怎么做呢?

由于您只需要一次具有相同值的第一个元素以及相应的第二个元素的最大值,因此for every distinct 1st element in the tuple list, list the max 2nd element表述问题for every distinct 1st element in the tuple list, list the max 2nd element没有什么不同for every distinct 1st element in the tuple list, list the max 2nd element

val l = List(("a",1), ("a",2), ("b",2), ("b",1))

l.groupBy(_._1).
  mapValues( _.map(_._2).max ).
  toList
// res1: List[(String, Int)] = List((b,2), (a,2))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM