groovy转置2D阵列不同的大小 [英] groovy transpose 2d array different sizes
本文介绍了groovy转置2D阵列不同的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
def mtrx = [
[1,2,3],
[4,5,6,7]
]
预期结果:
[[1,4],[2,5],[3 ,6],[null,7]]
或
[[1,4],[2,5],[3,6],[7]]
pre>
方法.transpose()适用于大小相同的工作正常,但不相等 - 有些元素被截断。
我的代码是:
def max = 0
def map = [:]
def mapFinal = [:]
def row = 0
def mtrx = [
[1,2,3],
[4,5,6 ,7]
]
mtrx.each {it->
println it.size()
if(maxmax = it.size()
}
}
def转置= mtrx.each {it->
println it
it.eachWithIndex {it1,index->
println it1 +'row'+ row +'column'+ index
mapFinal [row +''+ index] = it1
map [index +''+ row] = it1
}
row ++
}
println map
println mapFinal
解决方案尝试
(0 ..<(mtrx * .size() .max()))。collect {
mtrx * .getAt(it)
}
With groovy I want to make a transpose on list of lists(with different sizes).
def mtrx = [ [1,2,3], [4,5,6,7] ]
expected result:
[[1,4],[2,5],[3,6],[null,7]]
or
[[1,4],[2,5],[3,6],[7]]
Method .transpose() is working for equal sized is working fine, but for not equal - some elements are cut off.
My code is:
def max = 0 def map = [:] def mapFinal = [:] def row = 0 def mtrx = [ [1,2,3], [4,5,6,7] ] mtrx.each{it-> println it.size() if(max < it.size()){ max = it.size() } } def transposed = mtrx.each{it-> println it it.eachWithIndex{it1, index-> println it1 + ' row ' + row + ' column ' +index mapFinal[row+''+index] = it1 map[index+''+row] = it1 } row++ } println map println mapFinal
解决方案Try
(0..<(mtrx*.size().max())).collect { mtrx*.getAt(it) }
这篇关于groovy转置2D阵列不同的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文