如何红宝石多个大小相同的阵列组合 [英] How to combine multiple arrays of the same size in ruby
本文介绍了如何红宝石多个大小相同的阵列组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有3个或多个阵列我想合并成一个,我该怎么做,在红宝石?难道是上拉链的变化?
例如,我有
A = [1,2,3]
B = [4,5,6]
C = [7,8,9]
和我想有一个数组,看起来像
[[1,4,7],[2,5,8],[3,6,10]]
解决方案
[A,B,C] .transpose
是你所需要的。我preFER这拉链
的时间的50%。
If I have 3 or more arrays I want to combine into one, how do I do that in ruby? Would it be a variation on zip?
For example, I have
a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]
and I would like to have an array that looks like
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
解决方案
[a,b,c].transpose
is all you need. I prefer this to zip
50% of the time.
这篇关于如何红宝石多个大小相同的阵列组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文