How to connect two dictionary together?

Hi there,

Do you know of any easy way of connecting two or more tuples together? A simple example that explains one of the thing I need is this situation:

I have a dictionary that maps a set of pairs to a value (1,2) => 10 or (1,8) => 7. I have another vector of triples that the first two elements are all intersect with previous pairs; like (1,2,1) (1,2,2) (1,8,1) . So is there a simple method that I can map the values of first pairs to triples regardless of their third component? The issue is that the length of pairs are not the same as the length of triples. In some instances pairs are greater and in others not. But the way I have to think on mapping would be similar, yes?

for instance

# Pair_dict:   Dict{Tuple{Int64, Int64}, Float64} 
Prices = Dict(all_pairs .=> price)  # where all_pairs  and price are number 
(1,2) => 10
(1,8) => 7
(2,2) => 9
(2,5) => 8
(2,7) =>4
(3,1) => 9
(3,6) =>5

Then triples might be

# the third component are a vector of costumers [ 1,2,7] for example
Orders = [(1,2,1) , (1,2,2), (1,8,1), (1,8,2), (2,7,1), (2,5,1), (3,1,1), (3,1,7)]  

how to say (1,2,1) => 10 , (1,2,2) => 10 and so (1,2,anything) => 10

How about this?

Prices = Dict([
(1,2) => 10
(1,8) => 7
(2,2) => 9
(2,5) => 8
(2,7) =>4
(3,1) => 9
(3,6) => 5
])

Orders = [(1,2,1) , (1,2,2), (1,8,1), (1,8,2), (2,7,1), (2,5,1), (3,1,1), (3,1,7)]

Price_Orders = Dict([(a,b,c) => Prices[(a,b)] for (a,b,c) in Orders])
2 Likes

Following @jd-foster’s answer: if the number of orders (length(Orders)) is large, then turning the array comprehension into a generator could bring a reasonable speed up.

Price_Orders = Dict([(a,b,c) => Prices[(a,b)] for (a,b,c) in Orders]) #before
Price_Orders = Dict((a,b,c) => Prices[(a,b)] for (a,b,c) in Orders) #after
3 Likes