'Filtering out some element of a vector of tuples

I have a very big vector of tuples and need to extract some of them based on the some criteria (conditions).

That is, if the first and second elements of the tuple belongs to the other vectors we want to save them otherwise not. For example, considering the following where we have two vectors of vectors (i.e. starts and valid)

my_tuple = [(1, 2), (1, 3), (1, 5), (2, 3), (3, 4), (3, 5), (4, 1), (4, 5), (5, 2)]
starts = [[2, 8, 3, 4] , [1,2]]
valid = [ [2,3,6,8], [1,3,4,5] ]

How can I only have those tuples in my_links where their i belongs to start and their j belongs to valid?

The desired output should be a vector of tuples like the following:

my_tuple = [(1, 2), (1, 3), (1, 5), (2, 3), (3, 4), (3, 5), (4, 1), (4, 5), (5, 2)]
starts = [[2, 8, 3, 4] , [1,2]]
valid = [ [2,3,6,8], [1,3,4,5] ]
##############Would like an OTUPUT like:

[ [(2,3)], [(1,3) , (1,5), (2,3)] ] 

Basically, what I'm trying to filter is that going through each pairs of vector ( in this case we have to pair[2, 8, 3, 4] and [2,3,6,8] -- [1,2] and [1,3,4,5] ) in start and valid vectors and then having those combination of them that make a tuple in my_tuples

I've tried to issue these commands but it's not useful, what can you suggest?

new_tuple = [(i,j) for (i,j) in my_links if ( i in starts && j in valid) ]
new_tuple = [(i,j) for (i,j) in my_links if ( i in starts , j in valid) ]

enter image description here



Solution 1:[1]

Is this what you want?

julia> [filter(t -> t[1] in starts[i] && t[2] in valid[i], my_tuple) for i in eachindex(starts, valid)]
2-element Vector{Vector{Tuple{Int64, Int64}}}:
 [(2, 3)]
 [(1, 3), (1, 5), (2, 3)]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bogumił Kamiński