如何在ruby中将数组的每个元素压缩到另一个数组的每个元素?

时间:2018-11-10 20:37:50

标签: arrays ruby merge zip

说我有这个数组:

let iteems = declass (id: (idl as! String?)! , piecename: (namepiece as! String?)! , pieceinfo: (infopiece as! String?)! , imagepath : (image1 as! String?)!)

如何合并它们以获得:

[0, 1, 4], [2, 3]

我尝试过:

[0,2], [0,3], [1,2], [1,3], [4,2], [4,3]

但是我得到了

[0, 1, 4].zip [2, 3]

有什么想法吗?

1 个答案:

答案 0 :(得分:4)

[0, 1, 4].product([2, 3])

应生成:

[[0, 2], [0, 3], [1, 2], [1, 3], [4, 2], [4, 3]]