Dict 动态新建key值无法自动初始化为零 导致自加错误(注:这不是Julia本身问题)


#1

#---------代码---------------
dx=Dict()

for i in 1:100
x=rand(1:100)
dx[x]+=1
end

println(dx)
#--------------------------
运行报错:
ERROR: LoadError: KeyError: key 38 not found


#2
dx[x]+=1

你可以理解为

dx[x]=dx[x]+1

问题出在右边的dx[x],没有x这个key


#3

dx=Dict()

for i in 1:100
x=rand(1:100)
if haskey(dx,x)==false; dx[x]=0; end # 须得补充这句
dx[x]+=1
end

println(dx)


#4

对比一下
Julia:
if haskey(dx,x)==false; dx[x]=0; end

Perl:
$dx{x}=0 if !exists $dx{x} ;

说明:
perl有自己的关键词,julia几乎都是函数(包括 println 等等)
所以,学习julia,特别需要有一张函数表供查阅。


#5
julia> methodswith(Dict)
[1] serialize(s::Serialization.AbstractSerializer, d::Dict) in Serialization at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.3\Serialization\src\Serialization.jl:331
[2] copy(d::Dict) in Base at dict.jl:119
[3] delete!(h::Dict, key) in Base at dict.jl:651
[4] empty!(h::Dict{K,V}) where {K, V} in Base at dict.jl:264
[5] filter!(pred, h::Dict{K,V}) where {K, V} in Base at dict.jl:693
[6] get(h::Dict{K,V}, key, default) where {K, V} in Base at dict.jl:500
[7] get(default::Union{Function, Type}, h::Dict{K,V}, key) where {K, V} in Base at dict.jl:522
[8] get!(h::Dict{K,V}, key0, default) where {K, V} in Base at dict.jl:420
[9] get!(default::Union{Function, Type}, h::Dict{K,V}, key::K) where {K, V} in Base at dict.jl:447
[10] get!(default::Union{Function, Type}, h::Dict{K,V}, key0) where {K, V} in Base at dict.jl:439
[11] getindex(h::Dict{K,V}, key) where {K, V} in Base at dict.jl:476
[12] getkey(h::Dict{K,V}, key, default) where {K, V} in Base at dict.jl:568
[13] haskey(h::Dict, key) in Base at dict.jl:545
[14] isempty(t::Dict) in Base at dict.jl:681
[15] iterate(t::Dict) in Base at dict.jl:677
[16] iterate(t::Dict, i) in Base at dict.jl:679
[17] length(t::Dict) in Base at dict.jl:682
[18] pop!(h::Dict) in Base at dict.jl:613
[19] pop!(h::Dict, key) in Base at dict.jl:579
[20] pop!(h::Dict, key, default) in Base at dict.jl:608
[21] setindex!(h::Dict{K,V}, v0, key::K) where {K, V} in Base at dict.jl:380
[22] setindex!(h::Dict{K,V}, v0, key0) where {K, V} in Base at dict.jl:372
[23] sizehint!(d::Dict{T,V} where V, newsz) where T in Base at dict.jl:232
[24] launch(manager::Distributed.SSHManager, params::Dict, launched::Array, launch_ntfy::Base.GenericCondition{Base.AlwaysLockedST}) in Distributed at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.3\Distributed\src\managers.jl:125
[25] launch(manager::Distributed.LocalManager, params::Dict, launched::Array, c::Base.GenericCondition{Base.AlwaysLockedST}) in Distributed at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.3\Distributed\src\managers.jl:322

#6

楼上methodswith列举不全。漏掉了最基础的!!!特别补充一下

collect
keys
values


#7

methodswith默认不显示父类型的方法,如果你要全部的就加个参数。
另外collect不是针对Dict的,而是针对iterable对象的。
建议多看看文档,不要咋咋呼呼的

julia> methodswith(Dict;supertypes=true)
[1] serialize(s::Serialization.AbstractSerializer, d::Dict) in Serialization at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.3\Serialization\src\Serialization.jl:331
[2] ==(l::AbstractDict, r::AbstractDict) in Base at abstractdict.jl:440
[3] convert(::Type{T}, x::T) where T<:AbstractDict in Base at abstractdict.jl:484
[4] convert(::Type{T}, x::AbstractDict) where T<:AbstractDict in Base at abstractdict.jl:487
[5] copy(d::Dict) in Base at dict.jl:119
[6] copy(a::AbstractDict) in Base at abstractdict.jl:151
[7] copy!(dst::AbstractDict, src::AbstractDict) in Base at abstractdict.jl:152
[8] delete!(h::Dict, key) in Base at dict.jl:651
[9] empty(a::AbstractDict) in Base at abstractdict.jl:148
[10] empty(a::AbstractDict, ::Type{V}) where V in Base at abstractdict.jl:149
[11] empty(a::AbstractDict, ::Type{K}, ::Type{V}) where {K, V} in Base at dict.jl:166
[12] empty!(h::Dict{K,V}) where {K, V} in Base at dict.jl:264
[13] filter(f, d::AbstractDict) in Base at abstractdict.jl:389
[14] filter!(pred, h::Dict{K,V}) where {K, V} in Base at dict.jl:693
[15] filter!(f, d::AbstractDict) in Base at abstractdict.jl:348
[16] get(h::Dict{K,V}, key, default) where {K, V} in Base at dict.jl:500
[17] get(default::Union{Function, Type}, h::Dict{K,V}, key) where {K, V} in Base at dict.jl:522
[18] get!(h::Dict{K,V}, key0, default) where {K, V} in Base at dict.jl:420
[19] get!(default::Union{Function, Type}, h::Dict{K,V}, key::K) where {K, V} in Base at dict.jl:447
[20] get!(default::Union{Function, Type}, h::Dict{K,V}, key0) where {K, V} in Base at dict.jl:439
[21] getindex(h::Dict{K,V}, key) where {K, V} in Base at dict.jl:476
[22] getindex(t::AbstractDict, key) in Base at abstractdict.jl:467
[23] getindex(t::AbstractDict, k1, k2, ks...) in Base at abstractdict.jl:476
[24] getkey(h::Dict{K,V}, key, default) where {K, V} in Base at dict.jl:568
[25] hash(a::AbstractDict, h::UInt64) in Base at abstractdict.jl:459
[26] haskey(h::Dict, key) in Base at dict.jl:545
[27] haskey(d::AbstractDict, k) in Base at abstractdict.jl:17
[28] in(p::Pair, a::AbstractDict) in Base at abstractdict.jl:20
[29] in(p::Pair, a::AbstractDict, valcmp) in Base at abstractdict.jl:20
[30] in(p, a::AbstractDict) in Base at abstractdict.jl:28
[31] isempty(t::Dict) in Base at dict.jl:681
[32] isequal(l::AbstractDict, r::AbstractDict) in Base at abstractdict.jl:426
[33] iterate(t::Dict) in Base at dict.jl:677
[34] iterate(t::Dict, i) in Base at dict.jl:679
[35] keys(a::AbstractDict) in Base at abstractdict.jl:98
[36] keytype(a::AbstractDict) in Base at abstractdict.jl:235
[37] length(t::Dict) in Base at dict.jl:682
[38] map(f, ::AbstractDict) in Base at abstractarray.jl:2101
[39] merge(d::AbstractDict, others::AbstractDict...) in Base at abstractdict.jl:284
[40] merge(combine::Function, d::AbstractDict, others::AbstractDict...) in Base at abstractdict.jl:314
[41] merge!(d::AbstractDict, others::AbstractDict...) in Base at abstractdict.jl:176
[42] merge!(combine::Function, d::AbstractDict, others::AbstractDict...) in Base at abstractdict.jl:215
[43] pairs(a::AbstractDict) in Base at abstractdict.jl:134
[44] pop!(h::Dict) in Base at dict.jl:613
[45] pop!(h::Dict, key) in Base at dict.jl:579
[46] pop!(h::Dict, key, default) in Base at dict.jl:608
[47] push!(t::AbstractDict, p::Pair) in Base at abstractdict.jl:479
[48] push!(t::AbstractDict, p::Pair, q::Pair) in Base at abstractdict.jl:480
[49] push!(t::AbstractDict, p::Pair, q::Pair, r::Pair...) in Base at abstractdict.jl:481
[50] setindex!(h::Dict{K,V}, v0, key::K) where {K, V} in Base at dict.jl:380
[51] setindex!(h::Dict{K,V}, v0, key0) where {K, V} in Base at dict.jl:372
[52] setindex!(t::AbstractDict, v, k1, k2, ks...) in Base at abstractdict.jl:477
[53] show(io::IO, ::MIME{Symbol("text/plain")}, t::AbstractDict{K,V}) where {K, V} in Base at show.jl:75
[54] show(io::IO, t::AbstractDict{K,V}) where {K, V} in Base at dict.jl:25
[55] sizehint!(d::Dict{T,V} where V, newsz) where T in Base at dict.jl:232
[56] summary(io::IO, t::AbstractDict) in Base at abstractdict.jl:34
[57] valtype(a::AbstractDict) in Base at abstractdict.jl:249
[58] values(a::AbstractDict) in Base at abstractdict.jl:123
[59] launch(manager::Distributed.SSHManager, params::Dict, launched::Array, launch_ntfy::Base.GenericCondition{Base.AlwaysLockedST}) in Distributed at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.3\Distributed\src\managers.jl:125
[60] launch(manager::Distributed.LocalManager, params::Dict, launched::Array, c::Base.GenericCondition{Base.AlwaysLockedST}) in Distributed at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.3\Distributed\src\managers.jl:322

#8

我是刚入这个坑的,不知选择正确没有:blush: 请多关照:blush: