Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to merge dicts where the values are dicts

Tags:

julia

I have two dicts of keys and values as dicts too. I am trying to merge those dicts in a way where the values are appended in the keys.

Dict{String, Dict{String, String}} with 10 entries:
  "8"  => Dict("white-space"=>"nowrap", "text-align"=>"left !important")
  "4"  => Dict("white-space"=>"nowrap", "text-align"=>"left !important")
  "1"  => Dict("white-space"=>"nowrap", "text-align"=>"left !important")
  "24" => Dict("textAlign"=>"left !important")
  "5"  => Dict("white-space"=>"nowrap", "text-align"=>"left !important")
  "2"  => Dict("white-space"=>"nowrap", "text-align"=>"left !important")
  "6"  => Dict("white-space"=>"nowrap", "text-align"=>"left !important")
  "7"  => Dict("white-space"=>"nowrap", "text-align"=>"left !important")
  "9"  => Dict("white-space"=>"nowrap", "text-align"=>"left !important")
  "3"  => Dict("white-space"=>"nowrap", "text-align"=>"left !important")
Dict{String, Dict{String, String}} with 2 entries:
  "2" => Dict("min-width"=>"58px !important", "max-width"=>"58px !important")
  "3" => Dict("min-width"=>"58px !important", "max-width"=>"58px !important")

I want to merge those Dicts in a way where the values of the same keys are appended.

Dict{String, Dict{String, String}} with 10 entries:
  "8"  => Dict("white-space"=>"nowrap", "text-align"=>"left !important")
  "4"  => Dict("white-space"=>"nowrap", "text-align"=>"left !important")
  "1"  => Dict("white-space"=>"nowrap", "text-align"=>"left !important")
  "24" => Dict("textAlign"=>"left !important")
  "5"  => Dict("white-space"=>"nowrap", "text-align"=>"left !important")
  "2"  => Dict("white-space"=>"nowrap", "text-align"=>"left !important", "min-width"=>"58px !important", "max-width"=>"58px !important")
  "6"  => Dict("white-space"=>"nowrap", "text-align"=>"left !important")
  "7"  => Dict("white-space"=>"nowrap", "text-align"=>"left !important")
  "9"  => Dict("white-space"=>"nowrap", "text-align"=>"left !important")
  "3"  => Dict("white-space"=>"nowrap", "text-align"=>"left !important", "min-width"=>"58px !important", "max-width"=>"58px !important")
like image 302
andrdpedro Avatar asked Oct 31 '25 06:10

andrdpedro


1 Answers

Is this what you want?

julia> d1 = Dict(i => Dict(1 => i) for i in 1:3)
Dict{Int64, Dict{Int64, Int64}} with 3 entries:
  2 => Dict(1=>2)
  3 => Dict(1=>3)
  1 => Dict(1=>1)

julia> d2 = Dict(i => Dict(2 => -i) for i in 2:4)
Dict{Int64, Dict{Int64, Int64}} with 3 entries:
  4 => Dict(2=>-4)
  2 => Dict(2=>-2)
  3 => Dict(2=>-3)

julia> mergewith(merge, d1, d2)
Dict{Int64, Dict{Int64, Int64}} with 4 entries:
  4 => Dict(2=>-4)
  2 => Dict(2=>-2, 1=>2)
  3 => Dict(2=>-3, 1=>3)
  1 => Dict(1=>1)

(note though that it will silently use one of the values if the inner dicts have the same key)

like image 89
Bogumił Kamiński Avatar answered Nov 04 '25 19:11

Bogumił Kamiński



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!