如何倒转朱莉娅的字典?

Sco*_*hal 6 dictionary julia

如果我有一本字典如

my_dict = Dict(
    "A" => "one",
    "B" => "two",
    "C" => "three"
  )
Run Code Online (Sandbox Code Playgroud)

反转键/值映射的最佳方法是什么?

DSM*_*DSM 14

假设你不必担心碰撞钥匙,你可以使用重复的值mapreverse:

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"
Run Code Online (Sandbox Code Playgroud)

  • 我认为这在 Julia 版本 1.1.1 中不起作用。我收到错误“字典上未定义地图”。 (3认同)

Ale*_*ley 13

一种方法是使用理解来通过迭代键/值对来构建新字典,并沿途交换它们:

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

交换键和值时,您可能需要记住,如果my_dict有重复值(比如说"A"),那么新词典可能会有更少的键.此外,"A"新词典中按键定位的值可能不是您期望的值(Julia的词典不会以任何容易确定的顺序存储其内容).


ise*_*arn 5

不久前为可能有冲突值的字典做了这个

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
Run Code Online (Sandbox Code Playgroud)

如果某个键重复,则使用此选项,您将拥有一个包含所有值的列表,因此不会丢失任何数据,即

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]
Run Code Online (Sandbox Code Playgroud)


nan*_*nel 5

在 Julia 1.x 中(假设键和值之间存在双射):

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"
Run Code Online (Sandbox Code Playgroud)

否则:

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"]
Run Code Online (Sandbox Code Playgroud)