我有一個非常大的案例類集合,每個案例類都有一個 String 屬性和 Double 屬性,例如:
case class Sample(id:String, value: Double)
val samples: List[Sample] = List(
Sample("a", 0),
Sample("b", 2),
Sample("c", 20),
Sample("d", 50),
Sample("e", 100),
Sample("f", 1000)
)
給定一個桶串列,例如:
val buckets = List(5, 50, 100)
生成子集串列的最佳方法是什么,例如:
List(
List(Sample("a", 0)), // samples with Value of 0
List(Sample("b", 2)), // Samples with Value > 0 & <= 5
List(Sample("c", 20), Sample("d", 50)), // Samples with Value > 5 & <= 50
List(Sample("e", 100)), // Samples with Value > 50 & <= 100
List(Sample("f", 1000)), // Samples with Value > 100
)
uj5u.com熱心網友回復:
明確添加0
為桶邊界,使用二進制搜索快速找到正確的桶O(log(numBuckets))
,使用groupBy
:
val buckets = List[Double](0, 5, 50, 100)
val indexFinder: Double => Int = {
// Convert the list to a random-access structure for faster search
val arr = buckets.toArray
(value: Double) => arr.search(value).insertionPoint
}
samples.groupBy(sample => indexFinder(sample.value)).values.toList.foreach(println)
給出:
List(Sample(a,0.0))
List(Sample(b,2.0))
List(Sample(c,20.0), Sample(d,50.0))
List(Sample(e,100.0))
List(Sample(f,1000.0))
完整代碼:
case class Sample(id:String, value: Double)
val samples: List[Sample] = List(
Sample("a", 0),
Sample("b", 2),
Sample("c", 20),
Sample("d", 50),
Sample("e", 100),
Sample("f", 1000)
)
val buckets = List[Double](0, 5, 50, 100)
val indexFinder: Double => Int = {
val arr = buckets.toArray
(value: Double) => arr.search(value).insertionPoint
}
samples.groupBy(sample => indexFinder(sample.value)).values.toList.foreach(println)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/536743.html
標籤:斯卡拉