For example, input = [["a","b","c"],[1,2,3],["e","f","g"],...]
, the length inside is not limited; Now we need to form an array format data with a tuple as an element for each element in it. The format inside does not destroy the format of the original element.
when input
input = [["a","b","c"],[1,2,3],["e","f","g"]],
output
27-element Vector{Tuple{Char, Int64, String}}:
('a', 1, "e")
('a', 1, "f")
('a', 1, "g")
('a', 2, "e")
('a', 2, "f")
('a', 2, "g")
⋮
('c', 2, "f")
('c', 2, "g")
('c', 3, "e")
('c', 3, "f")
('c', 3, "g")
the code is as bellow:
function f(input)
output =[]
len = length(input)
if len == 1
return input
elseif len ==0
error("input is empty!")
end
for i = 1:len-1
if i == 1
output = g(input[1],input[i+1])
else
output = g(output,input[i+1])
end
end
return output
end
function g(a,b)
return [(i...,j) for i in a for j in b]
end
The above code is very general, but I think the usage of …
is very good. If there is no …
, it will become:
julia> f()
27-element Vector{Tuple{Tuple{String, Int64}, String}}:
(("a", 1), "e")
(("a", 1), "f")
(("a", 1), "g")
(("a", 2), "e")
(("a", 2), "f")
(("a", 2), "g")
⋮
(("c", 2), "f")
(("c", 2), "g")
(("c", 3), "e")
(("c", 3), "f")
(("c", 3), "g")
Obviously, this is not what we want. So, because of the addition of... so little, the result is amazing!
Further, we make the input more complex and test the results:
julia> arr_data = [["a","b","c"],[1,2,3],["e","f","g"],[1.0,2.0,5.0,6.0,7.0,8.0,9.0],["hello","world"],[["we"],["are"],["together!"]]]
6-element Vector{Vector{T} where T}:
["a", "b", "c"]
[1, 2, 3]
["e", "f", "g"]
[1.0, 2.0, 5.0, 6.0, 7.0, 8.0, 9.0]
["hello", "world"]
[["we"], ["are"], ["together!"]]
julia> f(arr_data)
1134-element Vector{Tuple{Char, Int64, String, Float64, String, Vector{String}}}:
('a', 1, "e", 1.0, "hello", ["we"])
('a', 1, "e", 1.0, "hello", ["are"])
('a', 1, "e", 1.0, "hello", ["together!"])
('a', 1, "e", 1.0, "world", ["we"])
('a', 1, "e", 1.0, "world", ["are"])
('a', 1, "e", 1.0, "world", ["together!"])
('a', 1, "e", 2.0, "hello", ["we"])
('a', 1, "e", 2.0, "hello", ["are"])
('a', 1, "e", 2.0, "hello", ["together!"])
('a', 1, "e", 2.0, "world", ["we"])
('a', 1, "e", 2.0, "world", ["are"])
('a', 1, "e", 2.0, "world", ["together!"])
('a', 1, "e", 5.0, "hello", ["we"])
⋮
('c', 3, "g", 7.0, "world", ["together!"])
('c', 3, "g", 8.0, "hello", ["we"])
('c', 3, "g", 8.0, "hello", ["are"])
('c', 3, "g", 8.0, "hello", ["together!"])
('c', 3, "g", 8.0, "world", ["we"])
('c', 3, "g", 8.0, "world", ["are"])
('c', 3, "g", 8.0, "world", ["together!"])
('c', 3, "g", 9.0, "hello", ["we"])
('c', 3, "g", 9.0, "hello", ["are"])
('c', 3, "g", 9.0, "hello", ["together!"])
('c', 3, "g", 9.0, "world", ["we"])
('c', 3, "g", 9.0, "world", ["are"])
('c', 3, "g", 9.0, "world", ["together!"])