我有List[(Int,Int)]
这样
{(1,5),(1,2),(1,7),(2,8),(2,3),(2,9),(3,10),(3,1),(3,12)}
我想要做的是在每个(1,5),(1,2),(1,7)
每个(2,8),(2,3),(2,9)
的第二个元素
和每个(3,10),(3,1),(3,12)
这样我们得到了一个结果:
List[Int]={14, 20, 23}
答案 0 :(得分:3)
使用System.Runtime.InteropServices.COMException: Creating an instance of the COM component with CLSID {00024500-0000-0000-C000-000000000046} from the IClassFactory failed due to the following error: 8001010a The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER)).
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
按大小拆分列表,映射第二个值和求和,如:
grouped
答案 1 :(得分:1)
您可以按元组的第一个元素对它们进行分组。然后,您将拥有一个Map
,其中包含每个不同的第一个元素的键和所有相关第二个元素的List
。
val myMap = myList.groupBy(_._1).mapValues(_.map(_._2))
现在您可以sum
第二个元素或应用所需的任何其他转换操作。
val mySums = myMap.mapValues(_.sum) //Map(2 -> 20, 1 -> 14, 3 -> 23)
您可以按照所需的顺序提取它们。
mySums.keys.toList.sorted.map(mySums) //res0: List[Int] = List(14, 20, 23)