Wednesday 30 March 2016

Julia: Dictionaries


Dictionary is a collection used to store elements in <key, value> pair. It is like map in Java.

How to create a dictionary
Dict{K,V}() : Construct a dictionary where keys are of type k, values are of type V.
julia> dict1=Dict{Int64,Float64}(1=>1.1, 2=>2.2, 3=>3.3)
Dict{Int64,Float64} with 3 entries:
  2 => 2.2
  3 => 3.3
  1 => 1.1

julia> dict1[1]
1.1

julia> dict1[2]
2.2

julia> dict1[3]
3.3


You can create a dictionary without specifying types also.
julia> dict1=Dict(1=>"Hari Krishna", 2=>23.4, 3=>true)
Dict{Int64,Any} with 3 entries:
  2 => 23.4
  3 => true
  1 => "Hari Krishna"

julia> dict1[1]
"Hari Krishna"

julia> dict1[2]
23.4

julia> dict1[3]
true
 
Check for key existence
haskey(collection, key) returns true if key exist in the collection, else false.
julia> names=Dict(1=>"Hari Krishna", 2=>"Sandesh", 3=>"Deeraj")
Dict{Int64,ASCIIString} with 3 entries:
  2 => "Sandesh"
  3 => "Deeraj"
  1 => "Hari Krishna"

julia> haskey(names, 1)
true

julia> haskey(names, 5)
false


Get the value associated with the key
get(collection, key, default) return the value associated with key, if there is no mapping associated with key, then default value is returned
julia> names
Dict{Int64,ASCIIString} with 3 entries:
  2 => "Sandesh"
  3 => "Deeraj"
  1 => "Hari Krishna"

julia> get(names, 1, "No name")
"Hari Krishna"

julia> get(names, 5, "No name")
"No name"


get(f::Function, collection, key) : return the value associated with key, if there is no mapping associated with key, then f() is returned.
julia> function get_default_data()
           return "No Name"
       end
get_default_data (generic function with 1 method)

julia> get(get_default_data, names, 1)
"Hari Krishna"

julia> get(get_default_data, names, 5)
"No Name"


get!(collection, key, default) : Return the value associated with this key, if no mapping exist for this key, then it create a mapping for given key with default value and return default value.
julia> names
Dict{Int64,ASCIIString} with 3 entries:
  2 => "Sandesh"
  3 => "Deeraj"
  1 => "Hari Krishna"

julia> 

julia> get!(names, 1, "PTR")
"Hari Krishna"

julia> names
Dict{Int64,ASCIIString} with 3 entries:
  2 => "Sandesh"
  3 => "Deeraj"
  1 => "Hari Krishna"

julia> 

julia> get!(names, 5, "PTR")
"PTR"

julia> names
Dict{Int64,ASCIIString} with 4 entries:
  2 => "Sandesh"
  3 => "Deeraj"
  5 => "PTR"
  1 => "Hari Krishna"


get!(f::Function, collection, key): Return the value associated with this key, if no mapping exist for this key, then it create a mapping for given key with value returned by function f and return the value returned by f().
julia> function get_default_name()
           return "PTR"
       end
get_default_name (generic function with 1 method)

julia> names
Dict{Int64,ASCIIString} with 4 entries:
  2 => "Sandesh"
  3 => "Deeraj"
  5 => "PTR"
  1 => "Hari Krishna"

julia> 

julia> get!(get_default_name, names, 1)
"Hari Krishna"

julia> names
Dict{Int64,ASCIIString} with 4 entries:
  2 => "Sandesh"
  3 => "Deeraj"
  5 => "PTR"
  1 => "Hari Krishna"

julia> 

julia> get!(get_default_name, names, 4)
"PTR"

julia> names
Dict{Int64,ASCIIString} with 5 entries:
  4 => "PTR"
  2 => "Sandesh"
  3 => "Deeraj"
  5 => "PTR"
  1 => "Hari Krishna"


getkey(collection, key, default): Return key if it exist in the dictionary, else return default.
julia> names
Dict{Int64,ASCIIString} with 5 entries:
  4 => "PTR"
  2 => "Sandesh"
  3 => "Deeraj"
  5 => "PTR"
  1 => "Hari Krishna"

julia> getkey(names, 1, "No Name")
1

julia> getkey(names, 7, "No Name")
"No Name"


Delete mapping for given key
delete!(collection, key): Delete mapping for given key in the collection and return the collection.
julia> names
Dict{Int64,ASCIIString} with 5 entries:
  4 => "PTR"
  2 => "Sandesh"
  3 => "Deeraj"
  5 => "PTR"
  1 => "Hari Krishna"

julia> delete!(names, 4)
Dict{Int64,ASCIIString} with 4 entries:
  2 => "Sandesh"
  3 => "Deeraj"
  5 => "PTR"
  1 => "Hari Krishna"

julia> delete!(names, 6)
Dict{Int64,ASCIIString} with 4 entries:
  2 => "Sandesh"
  3 => "Deeraj"
  5 => "PTR"
  1 => "Hari Krishna"


pop!(collection, key[, default]) : Delete and return the mapping of a key in the collection. If key don't exist, it return the default value.
julia> names
Dict{Int64,ASCIIString} with 4 entries:
  2 => "Sandesh"
  3 => "Deeraj"
  5 => "PTR"
  1 => "Hari Krishna"

julia> pop!(names, 5, "No Name")
"PTR"

julia> pop!(names, 4, "No Name")
"No Name"


Get keys in the dictionary
keys(collection) : Returns an iterator over keys in a collection.
julia> names
Dict{Int64,ASCIIString} with 3 entries:
  2 => "Sandesh"
  3 => "Deeraj"
  1 => "Hari Krishna"

julia> 

julia> for key in keys(names)
           println(key, ":", names[key])
       end
2:Sandesh
3:Deeraj
1:Hari Krishna


Get all values in the dictionary
values(collection) : Returns an iterator over the values in a collection.
julia> names
Dict{Int64,ASCIIString} with 3 entries:
  2 => "Sandesh"
  3 => "Deeraj"
  1 => "Hari Krishna"

julia> for value in values(names)
           println(value)
       end
Sandesh
Deeraj
Hari Krishna


Iterate over a dictionary using for loop
I already explained how to iterate over a dictionary using key(d), values(d) functions. By using for loop, you can get <K, V> at a time.
julia> names
Dict{Int64,ASCIIString} with 3 entries:
  2 => "Sandesh"
  3 => "Deeraj"
  1 => "Hari Krishna"

julia> 

julia> for (K, V) in names
           println(K,":",V)
       end
2:Sandesh
3:Deeraj
1:Hari Krishna


Get array of keys and values
collect(keys(d)): Return an array of keys
collect(values(d)) : Return an array of values.
julia> names
Dict{Int64,ASCIIString} with 3 entries:
  2 => "Sandesh"
  3 => "Deeraj"
  1 => "Hari Krishna"

julia> arr=collect(keys(names))
3-element Array{Int64,1}:
 2
 3
 1

julia> arr=collect(values(names))
3-element Array{ASCIIString,1}:
 "Sandesh"     
 "Deeraj"      
 "Hari Krishna"


Merge collections
merge(collection, otherCollections...) : Construct a merged collection with given collections. If the same key exists in more than one collection, then the value for that key will be the value it has in the last collection listed.
julia> names1=Dict(1=>"Hari", 2=>"Krishna", 3=>"Kishore", 4=>"Sudheer")
Dict{Int64,ASCIIString} with 4 entries:
  4 => "Sudheer"
  2 => "Krishna"
  3 => "Kishore"
  1 => "Hari"

julia> names2=Dict(3=>"Sreeni", 4=>"Mohan", 5=>"Sandip", 6=>"Sunil")
Dict{Int64,ASCIIString} with 4 entries:
  4 => "Mohan"
  3 => "Sreeni"
  5 => "Sandip"
  6 => "Sunil"

julia> merge(names1, names2)
Dict{Int64,ASCIIString} with 6 entries:
  4 => "Mohan"
  2 => "Krishna"
  3 => "Sreeni"
  5 => "Sandip"
  6 => "Sunil"
  1 => "Hari"

julia> merge(names2, names1)
Dict{Int64,ASCIIString} with 6 entries:
  4 => "Sudheer"
  2 => "Krishna"
  3 => "Kishore"
  5 => "Sandip"
  6 => "Sunil"
  1 => "Hari"

julia> names1
Dict{Int64,ASCIIString} with 4 entries:
  4 => "Sudheer"
  2 => "Krishna"
  3 => "Kishore"
  1 => "Hari"

julia> names2
Dict{Int64,ASCIIString} with 4 entries:
  4 => "Mohan"
  3 => "Sreeni"
  5 => "Sandip"
  6 => "Sunil"


Merge and update a collection
merge!(collection, others...): Update collection from other collections.
julia> names1
Dict{Int64,ASCIIString} with 4 entries:
  4 => "Sudheer"
  2 => "Krishna"
  3 => "Kishore"
  1 => "Hari"

julia> names2
Dict{Int64,ASCIIString} with 4 entries:
  4 => "Mohan"
  3 => "Sreeni"
  5 => "Sandip"
  6 => "Sunil"

julia> merge!(names1, names2)
Dict{Int64,ASCIIString} with 6 entries:
  4 => "Mohan"
  2 => "Krishna"
  3 => "Sreeni"
  5 => "Sandip"
  6 => "Sunil"
  1 => "Hari"

julia> names1
Dict{Int64,ASCIIString} with 6 entries:
  4 => "Mohan"
  2 => "Krishna"
  3 => "Sreeni"
  5 => "Sandip"
  6 => "Sunil"
  1 => "Hari"

julia> names2
Dict{Int64,ASCIIString} with 4 entries:
  4 => "Mohan"
  3 => "Sreeni"
  5 => "Sandip"
  6 => "Sunil"


Hint the capacity for a dictionary
sizehint!(s, n) : By using sizehint function, you can reserve the capacity for n elements. By pre reserving the memory improve the performance.

Get the key and value types
keytype(collection) : Return the type of keys
valtype(collection) : Return the type of values
julia> names
Dict{Int64,ASCIIString} with 3 entries:
  2 => "Sandesh"
  3 => "Deeraj"
  1 => "Hari Krishna"

julia> keytype(names)
Int64

julia> valtype(names)
ASCIIString




Previous                                                 Next                                                 Home

No comments:

Post a Comment