Iterate over collection of tuples


#1

Hi folks,

I am new to Julia and we are developing an open source framework for developing and using energy system optimization models. We are using JuMP. We have all been working mostly with GAMS untill now.

We are using a generic entity-relationship data model and I would like to develop the most efficient/compact way of iterating over a set of tuples (relationships).

For example, I have a set of entities of type e1 and another set of entities of type e2. I define a relationship that relates entities of type e1 to entites of type e2 which will essentially be a collection of pairs of entites of types e1 and e2.

Now I want to iterate over these pairs of entities… so something like

for o1,o2 in rel_e1_to_e2

end

so what does rel_e1_to_e2 look like and what is the proper syntax of the for loop statement?

Thanks in advance!


#2

Do you mean zip?


#3

As I understand it, zip will take two collections and combine them… i.e. turn 1,2,3,4 and a,b,c,d into:
(1,a)
(2,b)
(3,c)
(4,d)

Instead, I want to create something that contains only specific pairs and then iterate over them. E.g.

(2,e)
(4,a)


#4

Could you give a concrete example where the data structures are defined.

A vector of tuples seems to fit in with what you want but that seems to “easy”?

julia> V = [("a",3), ("b",5)]
2-element Array{Tuple{String,Int64},1}:
 ("a", 3)
 ("b", 5)

julia> for (x, i) in V
           @show x, i
       end
(x, i) = ("a", 3)
(x, i) = ("b", 5)