"one", "B" => "two", "C" => "three" ) What is...">

How to cancel a dictionary in Julia?

If I have a dictionary, for example

my_dict = Dict( "A" => "one", "B" => "two", "C" => "three" ) 

What is the best way to change the mapping of keys / values?

+6
dictionary julia-lang julia
source share
4 answers

One way would be to use understanding to create a new dictionary, iterate over key / value pairs, changing them in the following way:

 julia> Dict(value => key for (key, value) in my_dict) Dict{String,String} with 3 entries: "two" => "B" "one" => "A" "three" => "C" 

When exchanging keys and values, you can keep in mind that if my_dict has duplicate values ​​(for example, "A" ), then the new dictionary may have fewer keys. Also, the value found in the "A" key in the new dictionary may not match what you expect (Julia dictionaries do not store their contents in any easily defined order).

+12
source share

Assuming you don't have to worry about duplicate values ​​encountered as keys, you can use map with reverse :

 julia> my_dict = Dict("A" => "one", "B" => "two", "C" => "three") Dict{String,String} with 3 entries: "B" => "two" "A" => "one" "C" => "three" julia> map(reverse, my_dict) Dict{String,String} with 3 entries: "two" => "B" "one" => "A" "three" => "C" 
+14
source share

did this some time ago for dictionaries that may have conflicting meanings

 function invert_dict(dict, warning::Bool = false) vals = collect(values(dict)) dict_length = length(unique(vals)) if dict_length < length(dict) if warning warn("Keys/Vals are not one-to-one") end linked_list = Array[] for i in vals push!(linked_list,[]) end new_dict = Dict(zip(vals, linked_list)) for (key,val) in dict push!(new_dict[val],key) end else key = collect(keys(dict)) counter = 0 for (k,v) in dict counter += 1 vals[counter] = v key[counter] = k end new_dict = Dict(zip(vals, key)) end return new_dict end 

using this if if the key becomes duplicated, you will have a list with all the values, so the data will not be lost, i.e.

 julia> a = [1,2,3] julia> b = ["a", "b", "b"] julia> Dict(zip(a,b)) Dict{Int64,String} with 3 entries: 2 => "b" 3 => "b" 1 => "a" julia> invert_dict(ans) Dict{String,Array} with 2 entries: "b" => Any[2,3] "a" => Any[1] 
+4
source share

In Julia 1.x (assuming a bijection between keys and values):

 julia> D = Dict("A" => "one", "B" => "two", "C" => "three") Dict{String,String} with 3 entries: "B" => "two" "A" => "one" "C" => "three" julia> invD = Dict(D[k] => k for k in keys(D)) Dict{String,String} with 3 entries: "two" => "B" "one" => "A" "three" => "C" 

Otherwise:

 julia> D = Dict("A" => "one", "B" => "three", "C" => "three") Dict{String,String} with 3 entries: "B" => "three" "A" => "one" "C" => "three" julia> invD = Dict{String,Vector{String}}() Dict{String,Array{String,1}} with 0 entries julia> for k in keys(D) if D[k] in keys(invD) push!(invD[D[k]],k) else invD[D[k]] = [k] end end julia> invD Dict{String,Array{String,1}} with 2 entries: "one" => ["A"] "three" => ["B", "C"] 
+1
source share

All Articles