我无法找到在Julia中执行以下操作的方法:
输入:x= [["1","2","3"],["4","5","6"],["7","8","9"]]
所需的输出:["1 4 7", "2 5 8","3 6 9"]
基本上,我想生成n个字符串,其中n是x数组的长度,每个字符串都采用如上所示的字符。
有没有办法做到这一点?
修改
经过一番思考后,我得到了以下解决方案,但我不认为这是理想的解决方案。如果我得到一些答案,我仍然很高兴
x= [["1","2","3"],["4","5","6"],["7","8","9"]]
y= hcat(x...)
foo(y) = mapreduce(x->string(x," "),string,y)
mapslices(foo,y,2)
答案 0 :(得分:7)
以这种方式:
julia> x= [["1","2","3"],["4","5","6"],["7","8","9"]]
3-element Array{Array{String,1},1}:
String["1","2","3"]
String["4","5","6"]
String["7","8","9"]
julia> [join(k, " ") for k in zip(x...)]
3-element Array{String,1}:
"1 4 7"
"2 5 8"
"3 6 9"
julia>
答案 1 :(得分:1)
我喜欢瑞克的单行,但我不得不说,我不确定你为什么要寻找一种“聪明”的方式。 For循环在julia中非常有效,这是一个简单的嵌套for循环:
julia> StrList = Array{String, 1}() # initialise an empty String Array
0-element Array{String,1}
julia> rows = length(x);
julia> for col in 1:length(x[1])
s = ""; # empty placeholder string
for row in 1:(rows-1);
s *= x[row][col] * " "; # add N-1 elements with space
end
s *= x[rows][col]; # add Nth element without space
push!(StrList, s);
end
julia> StrList
3-element Array{String,1}:
"1 4 7"
"2 5 8"
"3 6 9"