Creating value pairs from some vectors

Viewed 88

Suppose we have a set of pair like the following, and each pair comes with a value attached to it. For isntance the value of pair (1,2) is 2.5. There are another set p1 which represent a set of persons. Each person connected to set of pairs. For instance, for p in p1 their associated pairs are the first elemenst of p1_pairs (i.e. [(1, 2), (1, 7), (2, 3)].



Pairs  = [(1, 2), (1, 7), (2, 3), (2, 5),(3, 4), (3, 5), (4, 1), (4, 5), (5, 2)]
Values = [ 2.5,    4.1,    7.4,    5.6,   2.8,    3.7,    6.9,     0.2,    3.2 ]

P1 = ['p','q']
p1_pairs = [   [(1, 2), (1, 7), (2, 3)], 
               [(1, 2), (2, 3), (2, 5) ,(5, 2)] 
           ]

The question is, how to bulid up a parameter person_values such that it return the value of the associated pairs?

For instance person_values['p',(1, 2)] = 2.5 and person_values['q',(2, 3)] = 7.4. In other words person_values will be indexed over elemets of p1 and p1_pairs and then assign s the corresponding values to them based on their values

1 Answers

If I correctly understood your question you want this:

julia> pv = Dict( Pairs .=> Values);

julia> [getindex.(Ref(pv), vals) for vals in p1_pairs]
2-element Vector{Vector{Float64}}:
 [2.5, 4.1, 7.4]
 [2.5, 7.4, 5.6, 3.2]
Related