julia-dict 사전(제13강)

7237 단어 julia
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 base.keytype="" valtype="" base.get="" key="" default="" collection="" base.getkey="" base.keys="" base.values="" base.delete="" base.pop="" base.merge="" others::abstractdict...="" d::abstractdict="" bs::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
    """

좋은 웹페이지 즐겨찾기