Lacking keys/values in a dictionary

I have 3 dictionaries, A and B:

A = Dict("1"  => 0.1
  "3"  => 0.1
  "5"  => 0.1
  "7"  => 0.1
  "9"  => 0.1
  "11" => 0.1
  "13" => 0.1
  "15" => 0.1
  "17" => 0.1
  "19" => 0.1)

B = Dict("1"  => 0.2
  "5"  => 0.1
  "7"  => 0.1
  "9"  => 0.1
  "11" => 0.2
  "15" => 0.1
  "17" => 0.2)

C = Dict("1"  => 0.1
  "3"  => 0.2
  "7"  => 0.1
  "9"  => 0.2
  "13" => 0.1
  "15" => 0.1
  "17" => 0.1
  "19" => 0.1)

If I am going to get a vector/matrix of the values of A, I have to A = [collect(values(A))].

A = 1-element Vector{Vector{Any}}:
[0.10, 0.10, 0.10, 0.10, 0.10, 0.10, 0.10, 0.10, 0.10, 0.10]

If I do the same to Dictionary B:

B = 1-element Vector{Vector{Any}}:
[0.10, 0.10, 0.10, 0.10, 0.10, 0.10, 0.10]

I want to do the same to B and C but Dictionary B does not contain keys/values “3” => 0.1, “13” => 0.1, “19”=>0.1 and Dictionary C Dows not contain “5” => 0.1, “11” => 0.1. What should I do in order to assign ZEROS to the values of the keys in dictionary B and C not found in A. Like this:

New_B = 1-element Vector{Vector{Any}}:
[0.10, 0, 0.10, 0.10, 0.10, 0.10, 0, 0.10, 0.10, 0]

New_C = 1-element Vector{Vector{Any}}:
[0.10, 0.10, 0 , 0.10, 0.10, 0, 0.10, 0.10, 0.10, 0.10]

Thanks for the help!

use get(B, "3", 0). Check out ? get for the documentation.

1 Like

Is this what you have in mind:

julia> A = Dict(["1" => 1, "3" => 3, "5" => 5]);

julia> B = Dict(["1" => 11, "2" => 22, "5" => 55]);

julia> # Sorting. Otherwise the order of entries in `B_new` is indeterminate.
       keyA = sort(collect(keys(A)));

julia> B_new = [get(B, k, 0)  for k in keyA]
3-element Vector{Int64}:
 11
  0
 55

Or am I making it too complicated?

3 Likes