如何在 Julia 中迭代字典

How to iterate over Dict in Julia

我怎么能在 Julia 中做这样的事情(它在 Python 中)?

for key, value in dictionary.items(): # <-- I don't know how to iterate over keys and values of a dictionary in Julia
    print(key, value)

谢谢。

你可以直接迭代,但是你需要在 (key, value):

周围加一个括号
julia> dict = Dict(i => (i/10 + rand(1:99)) for i = 1:3)
Dict{Int64, Float64} with 3 entries:
  2 => 24.2
  3 => 29.3
  1 => 41.1

julia> for (k,v) in dict
         @show k v
       end
k = 2
v = 24.2
k = 3
v = 29.3
k = 1
v = 41.1

julia> p = first(dict)
2 => 24.2

julia> typeof(p)
Pair{Int64, Float64}

julia> (a, b) = p;

julia> b
24.2