Hi
I recently was in need to convert a NamedArray in a nested dictionary (for JSON export) and wrote the little (and probably very inefficient) function below. If this functionality is useful of others as well is could be added to the package.
function Dict(na::NamedArray)
ndim = length(size(na))
if ndim == 1
return Dict(names(na)[1][i] => na[i] for i in 1:length(na))
else
d = Dict()
for i in 1:size(na, 1)
idx = [i; fill(:, ndim-1)]
d[names(na)[1][i]] = Dict(na[idx...])
end
return d
end
end
Example
na = NamedArray(rand(3))
setnames!(na, ["sub_$i" for i in 1:3], 1)
setnames!(na, ["loss_$i" for i in 1:4], 2)
setnames!(na, ["tech_$i" for i in 1:5], 3)
nd = Dict(na)
na["sub_1", "loss_3", "tech_2"]
nd["sub_1"]["loss_3"]["tech_2"]
Hi I recently was in need to convert a
NamedArray
in a nested dictionary (for JSON export) and wrote the little (and probably very inefficient) function below. If this functionality is useful of others as well is could be added to the package.Example