Ruby 将数组从嵌套数组解析为字符串
假设我有嵌套数组,如:Ruby 将数组从嵌套数组解析为字符串,ruby,arrays,parsing,nested,Ruby,Arrays,Parsing,Nested,假设我有嵌套数组,如: nested = [ [0.5623507523876472, ["h", "e", "l", "l", "o"]], [0.07381531933500263, ["h", "a", "l", "l", "o"]], [0.49993338806153054, ["n", "i", "h", "a", "o"]], [0.6499234734532127, ["
nested = [
[0.5623507523876472, ["h", "e", "l", "l", "o"]],
[0.07381531933500263, ["h", "a", "l", "l", "o"]],
[0.49993338806153054, ["n", "i", "h", "a", "o"]],
[0.6499234734532127, ["k", "o", "n", "n", "i", "c", "h", "i", "w", "a"]]
]
最初我想把它转换成散列。但首先我必须将数组(上面的示例[“h”、“e”、“l”、“l”、“o”])转换为“hello”
所以我的问题是如何将嵌套的转换为:
[
[0.5623507523876472, "hello"],
[0.07381531933500263, "hallo"],
[0.49993338806153054, "nihao"],
[0.6499234734532127, "konnichiwa"]
]
如果不想销毁嵌套的源数组,
:
使用:
如果要销毁源数组nested
使用方法:
nested = [
[0.5623507523876472, ["h", "e", "l", "l", "o"]],
[0.07381531933500263, ["h", "a", "l", "l", "o"]],
[0.49993338806153054, ["n", "i", "h", "a", "o"]],
[0.6499234734532127, ["k", "o", "n", "n", "i", "c", "h", "i", "w", "a"]]
]
nested_map = nested.map { |a,b| [a,b.join] }
# => [[0.5623507523876472, "hello"],
# [0.07381531933500263, "hallo"],
# [0.49993338806153054, "nihao"],
# [0.6499234734532127, "konnichiwa"]]
nested = [
[0.5623507523876472, ["h", "e", "l", "l", "o"]],
[0.07381531933500263, ["h", "a", "l", "l", "o"]],
[0.49993338806153054, ["n", "i", "h", "a", "o"]],
[0.6499234734532127, ["k", "o", "n", "n", "i", "c", "h", "i", "w", "a"]]
]
nested.map! { |a,b| [a,b.join] }
# => [[0.5623507523876472, "hello"],
# [0.07381531933500263, "hallo"],
# [0.49993338806153054, "nihao"],
# [0.6499234734532127, "konnichiwa"]]