11
a = [1, 2, 3]
b = [4, 5, 6]

我如何将两个数组组合成一个二维数组?:

[[1, 4], [2, 5], [3, 6]]
4

2 回答 2

31

试试数组#zip

a.zip(b)
=> [[1,4],[2,5],[3,6]]
于 2012-08-17T18:33:26.137 回答
10

虽然zip显然是最直接的答案,但这也有效:

[a, b].transpose
=> [[1, 4], [2, 5], [3, 6]]
于 2012-08-17T18:50:30.033 回答