朱莉娅错误:地图未在字典中定义

cww*_*cww 2 dictionary julia

julia> hotcell2vocab = Dict([(cell, i-1+vocab_start)
                  for (i,cell)  in  enumerate(hotcell)]);

julia> vocab2hotcell = map(reverse, hotcell2vocab)
ERROR: map is not defined on dictionaries
Run Code Online (Sandbox Code Playgroud)

hotcell2vocab是 a Dict,有没有一种方法可以反转 a 中的键和值Dict

Bog*_*ski 5

这是你想要的?

julia> d = Dict(i => i+10 for i in 1:5)
Dict{Int64,Int64} with 5 entries:
  4 => 14
  2 => 12
  3 => 13
  5 => 15
  1 => 11

julia> d_rev = Dict(b=>a for (a,b) in d)
Dict{Int64,Int64} with 5 entries:
  14 => 4
  13 => 3
  11 => 1
  15 => 5
  12 => 2
Run Code Online (Sandbox Code Playgroud)

当然,这假设字典中的值是唯一的。

  • 对于“Set”和“Dict”类型,明确不允许使用“map”。也许您可以尝试定义它,但问题是这些类型不能保证迭代顺序,问题是此类“map”的返回值的类型应该是什么。最后,例如,有人可能只想映射“Dict”的值,而保持键完好无损。 (4认同)