我怎么能在 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)
謝謝你。
uj5u.com熱心網友回復:
你可以迭代,但你需要一個括號(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
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/404575.html
標籤:
上一篇:在Typescript中,如何將Array<T>轉換為Map<K,V>并推斷K和V如果T是元組[K,V]同時具有編譯時保護(如果不是)
下一篇:列印一對字典
