问题
While I traverse a nested Dict in Julia, it gives this Error:
ERROR: access to undefined reference
in next at dict.jl:567
Here is the code where you can reproduce this error:
a = [0,19620,7291,32633,9,32513,42455,10045,31964,42455,11767,54]
b = [14318,16405,19,18913,19,8141,18958,12336,7,16588,17358,30]
d = Dict()
for aa in a
for bb in b
if ! haskey(d,aa)
d[aa]=Dict()
end
d[aa][bb] = 0.5
end
end
for k1 in keys(d)
s =0.0
for k2 in keys(d[k1])
s+= d[k1][k2]
end
for k2 in keys(d[k1])
d[k1][k2] = d[k1][k2] / s
end
end
It's wired, if a = [0,1] b = [0,1], it works fine.
----Update-----
Actually, as long as array b has 11 distinct elements, the error would happen. Also, if
d[k1][k2] = d[k1][k2] / s
become
d[k1][k2] = d[k1][k2] * s
or any other operations, the error disappear.
Any ideas ?
回答1:
This issue has now been fixed in the development branch, and will be available in the prerelease editions as soon as they gets updated.
See: https://github.com/JuliaLang/julia/pull/5894/files
回答2:
The error probably comes from the fact that you modify the contents of d[k1]
while you are iterating through it so the key iterator fails after you've modified the contents. (Why it only happens with some operations, I can't say.)
Anyhow, it can be fixed by changing the problematic loop to
for k2 in collect(keys(d[k1]))
d[k1][k2] = d[k1][k2] / s
end
(I'm not sure that this is the best way to do it.)
来源:https://stackoverflow.com/questions/21926255/traverse-nested-dict-in-julia-lang