# Creating a tuple from combination of tuple and vectors

**URL:** <https://discourse.julialang.org/t/creating-a-tuple-from-combination-of-tuple-and-vectors/81705>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump, tuple, vector\
**Created:** [May 26, 2022, 9:22am UTC](https://discourse.julialang.org/t/creating-a-tuple-from-combination-of-tuple-and-vectors/81705 "2022-05-26T09:22:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![joxane7887](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@joxane7887](https://discourse.julialang.org/u/joxane7887)\
**Post date:** [May 26, 2022, 9:22am UTC](https://discourse.julialang.org/t/creating-a-tuple-from-combination-of-tuple-and-vectors/81705/1 "2022-05-26T09:22:49Z")

</div>

I’d like to combine three thing; a `Vector{Tuple{Int64, Int64}}`, `Vector{Vector{Int64}}`, and a `Vector{Int64}` under some condition to be able to create a new tuple; in other words; a `Vector{Tuple{Int64, Int64, Int64}}`. And then define a variable `x(i,j,k)` on them.

For example, if we have the following:

```julia
# A is the condition
A = [(1, 2), (1, 3),
       (2, 1), (2, 7), (2, 8), (2, 4), 
       (3, 4), (3,5), (4,3) (4,2), (4,6),(4,9),
       (5,6), (7,8), (7,10), (8,9), (8,10), (9,10)]

# These are thing that we want to combine them           
B = [2,9]
C = [[1,7,3,4], [4,10,1,2,6,7]]
D = [12,15]

```

We are interested in a tuples like `(i,j,k)` which satisfy these conditions: ` i in B` , `j in C`, `k in D` and `(i,j) in A`

In the above case, therefore will result in the following:

```julia
 
[ # for k =12 
     [(2,1,12), (2,7,12), (2,4,12)] , 
     # for k =15  
     [(9,10,15)]

  ]

```

I’ve trired to first filter out those combination of `(i,j)` which are satisfy three condition of ` i in B && j in C && (i,j) in A` and then put them together with additional index over `k in D`. But it throw an error every time.

Do you know of any better way?

---

<div class="post-metadata">

**Author:** ![Karthik-d-k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karthik-d-k/32/35438_2.png) [@Karthik-d-k](https://discourse.julialang.org/u/Karthik-d-k)\
**Post date:** [May 26, 2022, 3:22pm UTC](https://discourse.julialang.org/t/creating-a-tuple-from-combination-of-tuple-and-vectors/81705/2 "2022-05-26T15:22:05Z")

</div>

Hi @joxane7887, welcome to the community 🙂

By looking at the example that you have provided, i assumed `length(B) == length(C) == length(D)` and came up with the below impl →

```julia
for (idx, i) in enumerate(B)
    for j in C[idx]
        (i, j) in A && println((i, j, D[idx]))
    end
end

```

output →

```julia
(2, 1, 12)
(2, 7, 12)
(2, 4, 12)
(9, 10, 15)

```

I’m not sure if this is what you are expecting …!!

---

<div class="post-metadata">

**Author:** ![joxane7887](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@joxane7887](https://discourse.julialang.org/u/joxane7887)\
**Post date:** [May 26, 2022, 6:08pm UTC](https://discourse.julialang.org/t/creating-a-tuple-from-combination-of-tuple-and-vectors/81705/3 "2022-05-26T18:08:33Z")

</div>

Hi @Karthik-d-k Thanks. I’m a beginner, and didn’t know about enumerate. So, How is it possible to use `@variable` macro and define a `x(i,j,k)` with this indicies?

Why this one is incorrect?  
`@variable(model, x[(i, j, k) for (idx, i) in enumerate(B) for j in C[idx] && (i, j) in A for k in D[idx] ] >= 0, Bin) `

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [May 26, 2022, 7:48pm UTC](https://discourse.julialang.org/t/creating-a-tuple-from-combination-of-tuple-and-vectors/81705/4 "2022-05-26T19:48:37Z")

</div>

Is this question related to [Define variables on each elemnts of a vector of vectors](https://discourse.julialang.org/t/define-variables-on-each-elemnts-of-a-vector-of-vectors/81495)? It seems pretty similar.

```julia
A = [
    (1, 2), (1, 3), (2, 1), (2, 7), (2, 8), (2, 4), (3, 4), (3, 5), (4, 3), 
    (4, 2), (4, 6), (4, 9), (5, 6), (7, 8), (7, 10), (8, 9), (8, 10), (9, 10),
]
B = [2, 9]
C = [[1, 7, 3, 4], [4, 10, 1, 2, 6, 7]]
D = [12, 15]

using JuMP
model = Model()
sets = [(b, c, d) for (k, d) in enumerate(D) for b in B for c in C[k] if (b, c) in A]
@variable(model, x[sets])
x[(2, 7, 12)]

```

---

<div class="post-metadata">

**Author:** ![joxane7887](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@joxane7887](https://discourse.julialang.org/u/joxane7887)\
**Post date:** [May 26, 2022, 9:25pm UTC](https://discourse.julialang.org/t/creating-a-tuple-from-combination-of-tuple-and-vectors/81705/5 "2022-05-26T21:25:48Z")

</div>

Quite beautiful! @odow Super thanks for this and link. I didn’t know about that.  
The output of @Karthik-d-k is theone I’m trying to put in your format. `sets` contains those combination that satisfys all things and in your definition `7-element Vector{Tuple{Int64, Int64, Int64}}:` However, it should be `4-element Vector{Tuple{Int64, Int64, Int64}}:` or `like the original following format

```julia
[ # for k =12 
     [(2,1,12), (2,7,12), (2,4,12)] , 
     # for k =15  
     [(9,10,15)]

  ]

```

I was unable spot the issue in your code ( could the issue be because of `(b, c) in A`)?

---

<div class="post-metadata">

**Author:** ![Karthik-d-k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karthik-d-k/32/35438_2.png) [@Karthik-d-k](https://discourse.julialang.org/u/Karthik-d-k)\
**Post date:** [May 27, 2022, 2:43am UTC](https://discourse.julialang.org/t/creating-a-tuple-from-combination-of-tuple-and-vectors/81705/6 "2022-05-27T02:43:49Z")

</div>

This will give you `Vector{Tuple{Int64, Int64, Int64}}` →

```julia
[(i, j, D[idx]) for (idx, i) in enumerate(B) for j in C[idx] if (i, j) in A]

```
