2
$\begingroup$

There are two lists:

L1 = {{12, 12}, {7, 15}, {15, 0}, {0, 17}, {17, 8}, {8, 2}, {2, 
    0}, {0, 9}, {9, 0}, {0, 3}, {3, 0}, {0, 16}, {16, 18}, {18, 
    1}, {1, 16}, {16, 18}, {18, 4}, {4, 9}, {9, 3}, {3, 1}, {1, 
    1}, {1, 18}, {18, 14}, {14, 11}, {11, 10}, {10, 1}, {1, 9}, {9, 
    2}, {2, 0}, {0, 17}, {17, 19}, {19, 2}, {2, 20}, {20, 1}, {1, 
    6}, {6, 2}, {2, 15}, {15, 8}, {8, 4}, {4, 9}};

L2 = {14 -> 2, 2 -> 14, 9 -> 10, 20 -> 8, 5 -> 1, 9 -> 5, 19 -> 11, 
   2 -> 12, 18 -> 5, 0 -> 11, 15 -> 18, 19 -> 6, 14 -> 4, 9 -> 14, 
   0 -> 19, 1 -> 13, 13 -> 17, 15 -> 5, 13 -> 0, 12 -> 12};

The goal is to find if one of the pairs from L1 is in L2 and select it from L2. For example: I can see that {12,12} from L1 exists in L2 in the form of 12->12, so I need to select it. I can do it by hand like:

Select[L2, #1[[1]] == 12 && #1[[2]] == 12 &]
{12 -> 12}

But the goal is to go through all pairs in L1 and get a new list of all coincidences. So I need to expand my approach for a whole list, but don't know how to reach this goal.

$\endgroup$
  • 1
    $\begingroup$ Try with Select[L2, MemberQ[L1, List @@ #] &] $\endgroup$ – Gustavo Delfino Dec 10 '19 at 21:45
  • 2
    $\begingroup$ Or Rule @@@ Intersection[L1, List @@@ L2]. $\endgroup$ – Rohit Namjoshi Dec 10 '19 at 21:57
  • 3
    $\begingroup$ Intersection[Rule @@@ L1, L2] is basically the same as @Rohit's answer. $\endgroup$ – Carl Woll Dec 10 '19 at 21:58
  • $\begingroup$ Thanks for your help! $\endgroup$ – Rumato Dec 10 '19 at 22:20
2
$\begingroup$
Rule @@@ Union @@ Nearest[List @@@ L2, L1, {\[Infinity], 0}]
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.