【发布时间】:2017-06-13 18:12:50
【问题描述】:
我在ruby中有一个二维数组,尝试合并数据如下:
a = [["a","1"],["b","2"],["c","2"],["d","3"],["e","4"],["f","4"],["g","4"]]
我想把它变成
[["a"],["b c"],["d"],["e f g"]] //merge each letter together if they have the same "key"
谁能帮我弄清楚最有效的方法是什么?谢谢。
【问题讨论】:
-
请在询问之前尝试一下 Enumerable#group_by 并试一试
-
这是一种非常奇特的目标格式。
-
@tadman 我想知道 OP 是否意味着
[["a"], ["b", "c"], ["d"], ["e", "f", "g"]]。 -
“键”是否总是按顺序排列,如您的示例所示?
-
@JordanRunning 你认为
chunk_while是一种选择吗?