Let’s say I have two dictionaries with the same keys but different values and different ordering:
D1 = Dict("B" => 10, "A" => 15, "C" => 17)
D2 = Dict("C" => 14, "B" => 11, "A" =>0)
I want to order dictionary D2 with the same key ordering as in dictionary D1. So I want to get:
D2= "B" => 11
"A" => 0
"C" => 14
The Dict
type in Base
is unordered. If you care about order of elements you should use some other data structures, for example like OrderedDict
from OrderedCollections.jl
.
3 Likes
Thank you for your relpy!
I had ordered D1 based on its values using:
D1= sort(unordered_D1; byvalue=true)
Are you saying even that ordering is not kept?
So the idea was to order D1 first based on its values and then order D2 based on D1 keys.
Yes, that’s not reliable, because Dict
isn’t sorted, trying to later access the items you can get them in different order. Again, if order is really important you should use something else.
4 Likes
Another option is Dictionaries.jl
2 Likes