在朱莉娅,我有一个字典,可以包含其他字典,字符串/数字列表,字典列表,字符串/数字和范围.
我需要一个列表,其中包含其中包含的每个范围(如StepRange,FloatRange,UnitRange)的所有可能的字典组合.
例:
Dict{}("A" => Dict{}("B" => 1:1:3, "C" => 2), "B" => [Dict{}( "S" => 1:1.1:2.1)])
=>
[
Dict{}("A" => Dict{}("B" => 1, "C" => 2), "B" => [Dict{}( "S" => 1.1)]),
Dict{}("A" => Dict{}("B" => 2, "C" => 2), "B" => [Dict{}( "S" => 1.1)]),
Dict{}("A" => Dict{}("B" => 3, "C" => 2), "B" => [Dict{}( "S" => 1.1)]),
Dict{}("A" => Dict{}("B" => 1, "C" => 2), "B" => [Dict{}( "S" => 2.1)]),
Dict{}("A" => Dict{}("B" => 2, "C" => 2), "B" => [Dict{}( "S" => 2.1)]),
Dict{}("A" => Dict{}("B" => 3, "C" => 2), "B" => [Dict{}( "S" => 2.1)])
]
现在,我正在重载像这样的递归函数,但不知道如何继续.
function iterate(generic, nets::Array)
return (generic, false)
end
function iterate(range::Union{StepRange,FloatRange,UnitRange}, nets::Array)
return (collect(range), true)
end
function iterate(array::Array, nets::Array)
for (n, v) in enumerate(array)
res = iterate(v, nets)
if res[2]
## We found a range! Return it
return res
end
end
return (array, false)
end
function iterate(dict::Dict, nets::Array)
for (k, v) in dict
res = iterate(v, nets)
if res[2]
return (dict, true)
end
end
return (dict, false)
end
(我已经在python中完成了这个,但是处理一段文本,使用正则表达式来查找自定义范围(例如“[1,2,0.1]”)并在生成解析它的文本代码之后.)
最佳答案 下面的代码片段再现了示例中的输出,它可以作为其他变体的基础,这些变体以不同的方式处理递归(有很多选项,正如我在尝试这样做时注意到的那样).它使用与Pkg.add(“Iterators”)一起安装的Iterators.jl.
using Iterators
function findranges{K}(sd::Dict{K})
ranges = Vector{Vector}()
for v in values(sd)
if isa(v,Range)
push!(ranges,collect(v))
elseif isa(v,Dict)
push!(ranges,recdictcollect(v))
elseif isa(v,Vector)
push!(ranges,map(x->vcat(x...),collect(product(map(recdictcollect,v)...))))
end
end
ranges
end
function recdictcollect{K}(sd::Dict{K})
ranges = findranges(sd)
if length(ranges)==0
cases = [()]
else
cases = product(ranges...) |> collect
end
outv = Vector{Dict{K,Any}}()
for c in cases
newd = Dict{K,Any}()
i = 1
for (k,v) in sd
if any([isa(v,t) for t in [Range,Dict,Vector]])
newd[k] = c[i]
i += 1
else
newd[k] = v
end
end
push!(outv,newd)
end
return outv
end
这个例子:
julia> example = Dict{}("A" => Dict{}("B" => 1:1:3, "C" => 2), "B" => [Dict{}( "S" => 1:1.1:2.1)])
Dict{ASCIIString,Any} with 2 entries:
"B" => [Dict("S"=>1.0:1.1:2.1)]
"A" => Dict{ASCIIString,Any}("B"=>1:1:3,"C"=>2)
julia> recdictcollect(example)
6-element Array{Dict{ASCIIString,Any},1}:
Dict{ASCIIString,Any}("B"=>[Dict{ASCIIString,Any}("S"=>1.0)],"A"=>Dict{ASCIIString,Any}("B"=>1,"C"=>2))
Dict{ASCIIString,Any}("B"=>[Dict{ASCIIString,Any}("S"=>2.1)],"A"=>Dict{ASCIIString,Any}("B"=>1,"C"=>2))
Dict{ASCIIString,Any}("B"=>[Dict{ASCIIString,Any}("S"=>1.0)],"A"=>Dict{ASCIIString,Any}("B"=>2,"C"=>2))
Dict{ASCIIString,Any}("B"=>[Dict{ASCIIString,Any}("S"=>2.1)],"A"=>Dict{ASCIIString,Any}("B"=>2,"C"=>2))
Dict{ASCIIString,Any}("B"=>[Dict{ASCIIString,Any}("S"=>1.0)],"A"=>Dict{ASCIIString,Any}("B"=>3,"C"=>2))
Dict{ASCIIString,Any}("B"=>[Dict{ASCIIString,Any}("S"=>2.1)],"A"=>Dict{ASCIIString,Any}("B"=>3,"C"=>2))