文件行中的 Bin 编号可以优雅地分组
Bin numbers in line of file to group elegantly
我有一个 text-file,如下所示。它包含 6 行的 header,然后仅包含数值(整数)。我想将这些整数分到一个范围让我们假设值在 0 ... 50 范围内我想将它们分到 5 groups 即分到 0...10...20...30...40...50
是否有一些优雅的方法来对它们进行分类?
ncols 4
nrows 3
xllcorner 0
yllcorner 0
cellsize 1
nodata_value -999
1 2 3 4
4 21 3 3
3 2 31 1
目前,我对文件的行使用迭代器,并使用扫描仪对行内的值进行标记 - 这看起来相当笨拙。期待一些优雅的想法。
预期输出为
ncols 4
nrows 3
xllcorner 0
yllcorner 0
cellsize 1
nodata_value -999
1 1 1 1
1 2 1 1
1 1 3 1
object Test {
def main(args:Array[String]):Unit={
val source = scala.io.Source.fromFile("/ssd2/test.txt")
val lines = source.getLines().toList
val (header, payload) = (lines.take(6), lines.drop(6))
def classify(i:Int):Int = (i / 10) + 1
val binned = payload.map{line =>
line.split(" ").map(n => classify(n.toInt)).mkString(" ")
}
val result = header ++ binned
result.foreach(println(_))
}
}
虽然输出是
ncols 4
nrows 3
xllcorner 0
yllcorner 0
cellsize 1
nodata_value -999
1 1 1 1
1 3 1 1
1 1 4 1
我认为您的预期输出样本中存在一个小错误。
我有一个 text-file,如下所示。它包含 6 行的 header,然后仅包含数值(整数)。我想将这些整数分到一个范围让我们假设值在 0 ... 50 范围内我想将它们分到 5 groups 即分到 0...10...20...30...40...50
是否有一些优雅的方法来对它们进行分类?
ncols 4
nrows 3
xllcorner 0
yllcorner 0
cellsize 1
nodata_value -999
1 2 3 4
4 21 3 3
3 2 31 1
目前,我对文件的行使用迭代器,并使用扫描仪对行内的值进行标记 - 这看起来相当笨拙。期待一些优雅的想法。
预期输出为
ncols 4
nrows 3
xllcorner 0
yllcorner 0
cellsize 1
nodata_value -999
1 1 1 1
1 2 1 1
1 1 3 1
object Test {
def main(args:Array[String]):Unit={
val source = scala.io.Source.fromFile("/ssd2/test.txt")
val lines = source.getLines().toList
val (header, payload) = (lines.take(6), lines.drop(6))
def classify(i:Int):Int = (i / 10) + 1
val binned = payload.map{line =>
line.split(" ").map(n => classify(n.toInt)).mkString(" ")
}
val result = header ++ binned
result.foreach(println(_))
}
}
虽然输出是
ncols 4
nrows 3
xllcorner 0
yllcorner 0
cellsize 1
nodata_value -999
1 1 1 1
1 3 1 1
1 1 4 1
我认为您的预期输出样本中存在一个小错误。