如何在 Julia 中迭代 Dict

poo*_*oky 1 dictionary 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)
Run Code Online (Sandbox Code Playgroud)

谢谢。

mca*_*ott 5

您可以只迭代,但需要一个括号(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
Run Code Online (Sandbox Code Playgroud)