如何将整数列表转换为每个 bin 频率的地图? [英] How to convert list of integers to a map with frequency per bin?
本文介绍了如何将整数列表转换为每个 bin 频率的地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个数字列表:
Lets say I have a list of numbers:
val numbers = List(15, 30, 110, 140, 170, 210)
如何计算 100
的每个 bin 的整数数量以获得:
How can I count the number of integers per bin of a 100
in order to get:
Map(0 -> 2, 100 -> 3, 200 -> 1)
推荐答案
scala> List(1,2,3,101,330,302).groupBy(i => i/100)
.map {case (i,l) => (i*100,l.length)}
res1: scala.collection.immutable.Map[Int,Int] = Map(100 -> 1, 300 -> 2, 0 -> 3)
这篇关于如何将整数列表转换为每个 bin 频率的地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文