1.字典分类:
Dict 是标准字典
IdDict 键始终是对象标识
WeakKeyDict 键是对对象的弱引用,因此即使在哈希表中进行引用也可能被垃圾回收
ImmutableDict实现不可变链表的字典(无法删除但可用新值覆盖隐藏该值)
AbstractDict字典抽象类型
2.1.字典构建Base.Dict
Dict([itr])
Dict{KeyType,ValueType}()#构造标准字典
ImmutableDict(KV::Pair) #在不可变字典中为key => value对创建一个新条目
#用于(key => value) in dict查看此特定组合是否在属性集中
#构建字典
Dict([("A", 1), ("B", 2)]) #可迭代参数创建
Dict("A"=>1, "B"=>2) #传递参数对创建
Dict{String,Int32}("A"=>1, "B"=>2)
Dict(d[1] =>d[2] for d in zip("AB",[1,2]))#生成器创建
Dict(i => f(i) for i = 1:3)
2.2.字典操作相关函数
Base.haskey(collection, key) -> Bool# 确定字典中是否有给定映射key
Base.sizehint!(s, n) #建议集合s至少保留n元素容量;可提高性能
Base.keytype(T::Type{<:AbstractArray})#返回字数组类型,等于eltype的结果keys(...)
Base.keytype(A::AbstractArray) #返回数组类型
Base.keytype(type) #返回字典键类型
valtype(T::Type{<:AbstractArray}) #返回数组的值类型
valtype(A::AbstractArray) #返回数组的值类型
valtype(type) # 获取字典值类型
Base.get(collection, key, default) # 获取键对应的值,键不存返回默认值
Base.get(f::Function, collection, key)# 获取键对应的值,键不存返回函数;可用do来调用
Base.get!(collection, key, default) #获取键对应值,否则默认值并存储在字典中
Base.get!(f::Function, collection, key)
Base.getkey(collection, key, default)# 获取字典中的键或默认值
Base.keys(iterator) #获取字典键iter
Base.values(iterator) #获取字典值iter
Base.delete!(collection, key)# 删除集合中给定键映射(无key则不删除),返回集合
Base.pop!(collection, key[, default])# 删除集合中给定键映射;否则返回default(如无则抛异常)
Base.merge(d::AbstractDict, others::AbstractDict...)#字典合并,类型自动提升;重复键为未集合值;原字典不变
Base.merge!(d::AbstractDict, others::AbstractDict...)#字典更新合并
Base.merge(combine, d::AbstractDict, others::AbstractDict...)#字典合并,相同键的值用合并器合并
Base.merge!(combine, d::AbstractDict, others::AbstractDict...)#字典更新合并-仅更新首字典
Base.merge(d1::NamedTuple, bs::NamedTuple...)#命名元组合并;键名自左向右,值用最右边的替代
Base.merge(d1::NamedTuple, iterable)# 将键值对可迭代项解释为命名元组,然后执行合并
2.3.值对函数
Base.pairs(IndexLinear(), A)#构建值对iter(index,value) x = A[i] i是有效索引
Base.pairs(IndexCartesian(), A)
Base.pairs(IndexStyle(A), A)#构建值对iter(index,value) -本机索引样式任何一种 常用
Base.pairs(collection) #由集合构建值对iter
Base.enumerate(collection)#由集合构建值对iter-不管索引,始终1开始计数
3.实例:
实例1:字典属性类型
julia> d = Dict('a'=>2, 'b'=>3)
julia> haskey(d, 'a'),haskey(d, 'c')# (true, false)
julia> sizehint!(d, 1)
# Dict{Char,Int64} with 2 entries:
# 'a' => 2
# 'b' => 3
julia> keytype(Dict('a'=> Int(1) , 'b'=>3))
# Char
julia> valtype(d)
# Int64
julia> keytype([1, 2, 3])
# Int64
julia> keytype([1, 2, 3]) == Int
# true
julia> keytype([1 2; 3 4])
# CartesianIndex{2}
julia> valtype(["one", "two", "three"])
# String
实例2: 获取字典中元素
get(d, "a", -1) #-1
key='g' #必须和字典d键的类型相同Char
get!(d, key) do
100 #函数f(x)=100返回类型:必须和字典d值的类型相同
end
getkey(d, 'a', -1)# 'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
pop!(d, "d") #KeyError
pop!(d, 'g') #100
实例3:字典迭代
julia> k=keys(d)
"""
Base.KeySet for a Dict{Char,Int64} with 2 entries. Keys:
'a'
'b'
"""
julia> v=values(d)
"""
Base.ValueIterator for a Dict{Char,Int64} with 2 entries. Values:
2
3
"""
julia> for v in k
println(v)
end
"""
a
b
"""
julia> for v in v
println(v)
end
"""
2
3
"""
julia> collect(keys(d))
"""
2-element Array{Char,1}:
'a'
'b'
"""
julia> collect(values(d))
"""
2-element Array{Int64,1}:
2
3
"""
实例4.1:字典合并
d1 = Dict("x1" => 0.01, "x2" => 3.14)
d2 = Dict("x3" => 100, "x2" => 200)
merge(d1, d2)
"""
Dict{String,Float64} with 3 entries:
"x1" => 0.01
"x2" => 200.0
"x3" => 100.0
"""
merge(+, d1, d2)
"""
Dict{String,Float64} with 3 entries:
"x1" => 0.01
"x2" => 203.14
"x3" => 100.0
"""
================================================================================
实例4.2:字典更新合并
merge!(d1, d2);
d1
"""
Dict{Int64,Int64} with 3 entries:
"x1" => 0.01
"x2" => 200.0
"x3" => 100.0
"""
实例4.3:字典更新合并
d1 = Dict("x1" => 0.01, "x2" => 3.14)
merge!(+, d1, d2);
julia> d1
"""
Dict{Int64,Int64} with 3 entries:
"x1" => 0.01
"x2" => 203.14
"x3" => 100.0
"""
实例4.5:命名元组合并
merge((d1=1, d2=2, c=3), (d2=4, d3=5), (d3=5,d4=0,d2=10))# (d1 = 1, d2 = 10, c = 3, d3 = 5, d4 = 0)
merge((d1=1, d2=2, c=3), [:d2=>4, :d=>5]) # (d1 = 1, d2 = 4, c = 3, d = 5)
实例5:值对
实例5.1:构建值对
arr = ["x11" "x21"; "x12" "x22"; "x13" "x23"];
"""
3×2 Array{String,2}:
"x11" "x21"
"x12" "x22"
"x13" "x23"
"""
p0=enumerate(arr)
Base.Iterators.Enumerate{Array{String,2}}(["x11" "x21"; "x12" "x22"; "x13" "x23"])
p1= pairs(IndexStyle(arr), arr)
"""
pairs(IndexLinear(), ::Array{String,2}) with 6 entries:
1 => "x11"
2 => "x12"
3 => "x13"
4 => "x21"
5 => "x22"
6 => "x23"
"""
p2=pairs(arr)
"""
pairs(::Array{String,2}) with 6 entries:
CartesianIndex(1, 1) => "x11"
CartesianIndex(2, 1) => "x12"
CartesianIndex(3, 1) => "x13"
CartesianIndex(1, 2) => "x21"
CartesianIndex(2, 2) => "x22"
CartesianIndex(3, 2) => "x23"
"""
实例5.2:迭代
julia> for (index, value) in p0
println("$index $value")
end
"""
1 x11
2 x12
3 x13
4 x21
5 x22
6 x23
"""
julia> for (index, value) in p1
println("$index $value")
end
"""
1 x11
2 x12
3 x13
4 x21
5 x22
6 x23
"""
julia> s = view(arr, 1:2, :);
s = view(arr, 1:2, :);
julia> s
"""
2×2 view(::Array{String,2}, 1:2, :) with eltype String:
"x11" "x21"
"x12" "x22
"""
julia> for (index, value) in pairs(IndexStyle(s), s)
println("$index $value")
end
"""
CartesianIndex(1, 1) x11
CartesianIndex(2, 1) x12
CartesianIndex(1, 2) x21
CartesianIndex(2, 2) x22
"""
julia> for(index,value) in p2
println("$index $value")
end
"""
CartesianIndex(1, 1) x11
CartesianIndex(2, 1) x12
CartesianIndex(3, 1) x13
CartesianIndex(1, 2) x21
CartesianIndex(2, 2) x22
CartesianIndex(3, 2) x23
"""