Groovy 平均数组的优雅解决方案
Groovy elegant solution for avg array
str = """
0.6266197101540969 , 0.21279720289932263 , 0.7888811159800816 , 0.3374125902260934 , 0.8299999833106995 , 0.4300000071525574 , 0.6000000238418579 , 0.1599999964237213
0.6286013903734567 , 0.21750000088165203 , 0.780979019361776 , 0.33202797309918836 , 0.8299999833106995 , 0.4300000071525574 , 0.6000000238418579 , 0.1599999964237213
0.6211805507126782 , 0.20285714375121253 , 0.7563043448372163 , 0.32666666932562566 , 0.8299999833106995 , 0.4300000071525574 , 0.6000000238418579 , 0.1599999964237213
0.5912230165956689 , 0.20713235388564713 , 0.7197058783734546 , 0.31335821047202866 , 0.8299999833106995 , 0.4300000071525574 , 0.6000000238418579 , 0.1599999964237213
0.6073239363834891 , 0.21000000010145473 , 0.7486428560955184 , 0.3176595757827691 , 0.8070422478125129 , 0.41928571494562283 , 0.5272857129573822 , 0.18029411882162094
0.6049999973513711 , 0.204892086360952 , 0.7618382347418982 , 0.3195035475577023 , 0.8021985744753628 , 0.44760563193072733 , 0.5268794330933415 , 0.18000000185320075
0.6292380872226897 , 0.1993396225965248 , 0.7613461521955637 , 0.3325471729040146 , 0.8194392418192926 , 0.4333644839369248 , 0.5276415085174003 , 0.180841121927043
"""
我需要计算行中的项目数,为它创建数组,然后数组应该有每个列的总和
最终结果:
//no saying this is the actualt avg of the colunm just pointing out...
sumArray = [0.62,0.41,0.61, ....]
averageTests = returnAvg(data)
println(averageTests)
def returnAvg(tmpData){
tmpData = tmpData.replaceAll("( )+", "")
def avgs = []//will hold array of avg
numOfDaysTest=tmpData.split("\n").size()
for (line in tmpData.split("\n")){
index=0
for (value in line.split(",")){
if (avgs[index] == null){
avgs[index] = 0
}
tmpnum = value as Double
avgs[index] += tmpnum
index++
}
}
for (i=0; i<avgs.size(); i++){
tmp = (avgs[i]/numOfDaysTest).toString()
avgs[i] = tmp.substring(0,tmp.indexOf('.')+3)
}
return avgs
}
最终结果还不错,但我确定还有更优雅的方法吗?
这个 one-liner 当然可以进一步简化,但我还没有想出一个可读的。我相信你可以改进它。
def means = str.trim().split('\n')*.split(',').collect{it*.trim().collect{e -> new BigDecimal(e)}.withIndex()}.collect{e -> e.collect{ee->[(ee[1]): [ee[0]]].entrySet()}}.flatten().groupBy{it.key}.collectEntries{key, val -> [(key):val*.value.flatten().average()]}
根据您的测试输入,它会产生
[0:0.6155980983990644, 1:0.20778834435382369, 2:0.7596710859407870,
3:0.32559653419534601, 4:0.8212399996214238, 5:0.43146512277478637,
6:0.5688295357050794, 7:0.16873360404239284]
其中键是列索引,值是对应的均值。
str = """
0.6266197101540969 , 0.21279720289932263 , 0.7888811159800816 , 0.3374125902260934 , 0.8299999833106995 , 0.4300000071525574 , 0.6000000238418579 , 0.1599999964237213
0.6286013903734567 , 0.21750000088165203 , 0.780979019361776 , 0.33202797309918836 , 0.8299999833106995 , 0.4300000071525574 , 0.6000000238418579 , 0.1599999964237213
0.6211805507126782 , 0.20285714375121253 , 0.7563043448372163 , 0.32666666932562566 , 0.8299999833106995 , 0.4300000071525574 , 0.6000000238418579 , 0.1599999964237213
0.5912230165956689 , 0.20713235388564713 , 0.7197058783734546 , 0.31335821047202866 , 0.8299999833106995 , 0.4300000071525574 , 0.6000000238418579 , 0.1599999964237213
0.6073239363834891 , 0.21000000010145473 , 0.7486428560955184 , 0.3176595757827691 , 0.8070422478125129 , 0.41928571494562283 , 0.5272857129573822 , 0.18029411882162094
0.6049999973513711 , 0.204892086360952 , 0.7618382347418982 , 0.3195035475577023 , 0.8021985744753628 , 0.44760563193072733 , 0.5268794330933415 , 0.18000000185320075
0.6292380872226897 , 0.1993396225965248 , 0.7613461521955637 , 0.3325471729040146 , 0.8194392418192926 , 0.4333644839369248 , 0.5276415085174003 , 0.180841121927043
"""
我需要计算行中的项目数,为它创建数组,然后数组应该有每个列的总和
最终结果:
//no saying this is the actualt avg of the colunm just pointing out...
sumArray = [0.62,0.41,0.61, ....]
averageTests = returnAvg(data)
println(averageTests)
def returnAvg(tmpData){
tmpData = tmpData.replaceAll("( )+", "")
def avgs = []//will hold array of avg
numOfDaysTest=tmpData.split("\n").size()
for (line in tmpData.split("\n")){
index=0
for (value in line.split(",")){
if (avgs[index] == null){
avgs[index] = 0
}
tmpnum = value as Double
avgs[index] += tmpnum
index++
}
}
for (i=0; i<avgs.size(); i++){
tmp = (avgs[i]/numOfDaysTest).toString()
avgs[i] = tmp.substring(0,tmp.indexOf('.')+3)
}
return avgs
}
最终结果还不错,但我确定还有更优雅的方法吗?
这个 one-liner 当然可以进一步简化,但我还没有想出一个可读的。我相信你可以改进它。
def means = str.trim().split('\n')*.split(',').collect{it*.trim().collect{e -> new BigDecimal(e)}.withIndex()}.collect{e -> e.collect{ee->[(ee[1]): [ee[0]]].entrySet()}}.flatten().groupBy{it.key}.collectEntries{key, val -> [(key):val*.value.flatten().average()]}
根据您的测试输入,它会产生
[0:0.6155980983990644, 1:0.20778834435382369, 2:0.7596710859407870,
3:0.32559653419534601, 4:0.8212399996214238, 5:0.43146512277478637,
6:0.5688295357050794, 7:0.16873360404239284]
其中键是列索引,值是对应的均值。