我想遍历并打印 Julia 中字典的 (key, value) 对。我怎样才能做到这一点?
我在这里看到了如何在 Julia 中初始化字典,但我也想遍历它。
在 python 中,你可以像这样迭代字典:
dict1 = {'a':1, 'b':2}
for key, value in dict1.items():
print(key, value)
# -> a 1
# b 2
Run Code Online (Sandbox Code Playgroud)
你如何在朱莉娅身上做同样的事情?
我发现的最接近的是这个,但它并不是最佳的:
D = Dict("a"=>1, "b"=>2)
for a_pair in D
println(a_pair.first, a_pair.second)
end
Run Code Online (Sandbox Code Playgroud)