4
$\begingroup$

I want to find for each list within a list of lists what intersections occur when taking set intersection for each list in another list of lists.

Hopefully that makes sense.

I have tried

Outer[Intersection[#1, #2] &, p1, p2]
MapThread[Intersection, {p1, p2}] (* This works but only when p1 and p2 are same length *)

where p1 = {{1, 2, 6}, {1, 2, 4, 6}, {1, 2, 3, 5, 6}} and p2= {{3, 5, 6}} But no such luck. The output should show that 6 is the only number in common between the list in p2 and each list in p1. If p2 contains another list, then the comparision function should perform the same check as with the 1st list in p2 but only care about the intersections that happen between this 2nd list and all the lists in p1.

I also should mention that when no intersections occur between list i in p1 with list j in p2 then something need to denote this (like {})

Ideally I want and output like result = {{{6},{6},{6}},{*results for 2nd element in p2*}} etc.

$\endgroup$
1
  • 2
    $\begingroup$ Why is the third element of the desired result not {3,5,6}? $\endgroup$ Apr 15, 2020 at 21:43

3 Answers 3

2
$\begingroup$

From mobile, so not tested

Intersection@@#& /@ Tuples[{p1, p2}]

enter image description here

Update. Another solution, which preserves the inner/outer structure:

Table[Intersection[l1, l2], {l1, p1}, {l2, p2}]
$\endgroup$
7
  • 1
    $\begingroup$ Nice. yes this returns same as mine. But I'm realizing that I need to organize the intersections into subcollections when comparing against a fixed list within the list of lists. (think each set of intersections corresponds to the index of the outside loop). $\endgroup$
    – skyfire
    Apr 15, 2020 at 21:35
  • 1
    $\begingroup$ Table[Intersection[l1,l2], {l1, p1}, {l2, p2}] $\endgroup$
    – Victor K.
    Apr 15, 2020 at 21:40
  • 1
    $\begingroup$ Table is a poor man’s map ;) $\endgroup$
    – Victor K.
    Apr 15, 2020 at 21:47
  • $\begingroup$ perfect. I updated mine as well (the old fashioned way). Do you know how I can print this into a table to see the results a bit easier? (giving you the answer either way). $\endgroup$
    – skyfire
    Apr 15, 2020 at 21:47
  • 1
    $\begingroup$ You should be able to just write Intersection@@@Tuples[{p1, p2}] for the first example $\endgroup$
    – Lukas Lang
    Apr 16, 2020 at 6:50
4
$\begingroup$
Outer[Intersection, p1, p2, 1]

{{{6}}, {{6}}, {{3, 5, 6}}}

$\endgroup$
2
  • $\begingroup$ can't believe I only needed to add ,1] Welp won't be getting back those hours. $\endgroup$
    – skyfire
    Apr 16, 2020 at 0:53
  • 1
    $\begingroup$ Don't fret too much about that. Consider it a rite of passage. :) $\endgroup$ Apr 16, 2020 at 12:48
0
$\begingroup$

Here is the naive way (hoping for a more idiomatic, functional soln from someone).

p1 = {{1, 2, 6}, {1, 2, 4, 6}, {1, 2, 3, 5, 6}};
p2 = {{3, 5, 6}};
list = {}
Do[
 AppendTo[list, {}]
  Do[

   If[IntersectingQ[p1[[x]], p2[[y]]], 
    Intersection[p1[[x]], p2[[y]]] // AppendTo[list[[y]], #] &, 
    Nothing]

   , {x, 1, Length[p1]}]
 , {y, 1, Length[p2]}]
list
```
$\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.