gpt4 book ai didi

dictionary - 生成包含范围的字典的所有组合

转载 作者:行者123 更新时间:2023-12-02 07:36:54 25 4
gpt4 key购买 nike

在 julia 中,我有一个字典,可以包含其他字典、字符串/数字列表、字典列表、字符串/数字和范围。

我需要一个列表,其中包含其中包含的每个范围(如 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))

关于dictionary - 生成包含范围的字典的所有组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37433071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com