You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
functionDict(na::NamedArray)
ndim =length(size(na))
if ndim ==1returnDict(names(na)[1][i] => na[i] for i in1:length(na))
else
d =Dict()
for i in1:size(na, 1)
idx = [i; fill(:, ndim-1)]
d[names(na)[1][i]] =Dict(na[idx...])
endreturn d
endend
Example
na =NamedArray(rand(3))
setnames!(na, ["sub_$i"for i in1:3], 1)
setnames!(na, ["loss_$i"for i in1:4], 2)
setnames!(na, ["tech_$i"for i in1:5], 3)
nd =Dict(na)
na["sub_1", "loss_3", "tech_2"]
nd["sub_1"]["loss_3"]["tech_2"]
The text was updated successfully, but these errors were encountered:
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
The text was updated successfully, but these errors were encountered: