是否存在融合两个相同大小的字符串数组的各个元素的快捷方式?

时间:2012-03-25 22:20:21

标签: ruby arrays multidimensional-array

我有两个字符串数组

[["1,'Tambaú','Praça Santo António','Tambaú','12x0',2,'I','EM',12,6,5934,50"], 
 ["2,'Beira Rio','Av. Bei ra Rio, Prox. Av Odilon Coutinho','Beira Rio','12x0',2,'I','EM',12,0,7249,0"], 
 ["3,'Cabo Branco','Cabo Bra nco, Prox. Rua Alice de Almeida','Cabo Branco','12x0',2,'I','EO',12,0,4751,0"], 
 ["901,'teste','teste','test e','Mini-estação de demonstração',1,'I','EO',2,1,97,50"]
]

[["-7.1105596,-34.824085"],
 ["-7.1243144,-34.823312"],
 ["-7.1395805,-34.816446"],
 ["-7.1008926,-35.068359"]]

我可以通过循环和添加元素手动完成,但我想知道是否有人知道是否存在更智能的方式。

编辑:这是我想解决任何困惑的结果

[
 "1,'Tambaú','Praça Santo António','Tambaú','12x0',2,'I','EM',12,6,5934,50,-7.1105596,-34.824085",
 "2,' Beira Rio','Av. Beira Rio, Prox. Av Odilon Coutinho','Beira Rio','12x0',2,'I','EM',12,0,7249,0,-7.1243144, -34.823312",
 "3,'Cabo Branco','Cabo Branco, Prox. Rua Alice de Almeida','Cabo Branco','12x0',2,'I','EO',12, 0,4751,0,-7.1395805,-34.816446", 
 "901,'teste','teste','teste','Mini-estação de demonstração',1,'I','EO' ,2,1,97,50,-7.1008926,-35.068359"
]

这就是我现在正在做的事情

for i in 0...arr1.size
  newArr[i] = (arr1[i][0] + "," + arr2[i][0])
end

2 个答案:

答案 0 :(得分:1)

此解决方案将数组保留在数组中(Arr1将被修改!)

arr1 = [
  ["1,'Tambaú','Praça Santo António','Tambaú','12x0',2,'I','EM',12,6,5934,50"], 
  ["2,'Beira Rio','Av. Bei ra Rio, Prox. Av Odilon Coutinho','Beira Rio','12x0',2,'I','EM',12,0,7249,0"], 
  ["3,'Cabo Branco','Cabo Bra nco, Prox. Rua Alice de Almeida','Cabo Branco','12x0',2,'I','EO',12,0,4751,0"], 
  ["901,'teste','teste','test e','Mini-estação de demonstração',1,'I','EO',2,1,97,50"]
]

arr2 = [["-7.1105596,-34.824085"], ["-7.1243144,-34.823312"], ["-7.1395805,-34.816446"], ["-7.1008926,-35.068359"]]


arr1.each_with_index{|el,i|
  el.first << ',' 
  el.first << arr2[i].first
}

使用arr1.flatten!,您可以从数组数组中创建一个“普通”数组。

您可以在“融合”之前执行flatten

arr1 = [
  ["1,'Tambaú','Praça Santo António','Tambaú','12x0',2,'I','EM',12,6,5934,50"], 
  ["2,'Beira Rio','Av. Bei ra Rio, Prox. Av Odilon Coutinho','Beira Rio','12x0',2,'I','EM',12,0,7249,0"], 
  ["3,'Cabo Branco','Cabo Bra nco, Prox. Rua Alice de Almeida','Cabo Branco','12x0',2,'I','EO',12,0,4751,0"], 
  ["901,'teste','teste','test e','Mini-estação de demonstração',1,'I','EO',2,1,97,50"]
]
arr1.flatten!

arr2 = [["-7.1105596,-34.824085"], ["-7.1243144,-34.823312"], ["-7.1395805,-34.816446"], ["-7.1008926,-35.068359"]]
arr2.flatten!

arr1.each_with_index{|el,i|
  el << ','
  el << arr2[i]
}

答案 1 :(得分:1)

flattenzip,然后map一个join

arr1 = [
  ["1,'Tambaú','Praça Santo António','Tambaú','12x0',2,'I','EM',12,6,5934,50"], 
  ["2,'Beira Rio','Av. Bei ra Rio, Prox. Av Odilon Coutinho','Beira Rio','12x0',2,'I','EM',12,0,7249,0"], 
  ["3,'Cabo Branco','Cabo Bra nco, Prox. Rua Alice de Almeida','Cabo Branco','12x0',2,'I','EO',12,0,4751,0"], 
  ["901,'teste','teste','test e','Mini-estação de demonstração',1,'I','EO',2,1,97,50"]
]

arr2 = [
  ["-7.1105596,-34.824085"],
  ["-7.1243144,-34.823312"],
  ["-7.1395805,-34.816446"],
  ["-7.1008926,-35.068359"]
]

arr1.flatten.zip(arr2.flatten).map { |pair| pair.join(',') }
#=> ["1,'Tamba','Praa Santo Antnio','Tamba','12x0',2,'I','EM',12,6,5934,50,-7.1105596,-34.824085"],
#    "2,'Beira Rio','Av. Bei ra Rio, Prox. Av Odilon Coutinho','Beira Rio','12x0',2,'I','EM',12,0,7249,0,-7.1243144,-34.823312",
#    "3,'Cabo Branco','Cabo Bra nco, Prox. Rua Alice de Almeida','Cabo Branco','12x0',2,'I','EO',12,0,4751,0,-7.1395805,-34.816446",
#    "901,'teste','teste','test e','Mini-estao de demonstrao',1,'I','EO',2,1,97,50,-7.1008926,-35.068359"]