# How to find the same number in sublists of list

Intersection[Table[Table[Sqrt[p1]/r1,{r1,0.1,0.7,0.1}],{p1,0.5,5,0.5}]]


I am trying to find same numbers in the sublists. But Intersection is not working for this...

• What if you use exact numbers (e.g. 1/10) instead of inexact ones (e.g. 0.1)? Jul 31, 2015 at 21:12
• Still not working...I think the curly bracket is creating problem. Jul 31, 2015 at 21:20
• You use the wrong syntax for Intersection. See the difference between Intersection[{0, 1, 3}, {0, 10, 2}, {3, 0}] (good) and Intersection[{{0, 1, 3}, {0, 10, 2}, {3, 0}}] (bad). In the second case (your case) it works if you write instead Apply[Intersection, {{0, 1, 3}, {0, 10, 2}, {3, 0}}] or Intersection @@ {{0, 1, 3}, {0, 10, 2}, {3, 0}}. However in your case it seems the intersection is empty ! Jul 31, 2015 at 21:37
• How will it give a sorted list of the elements common in any two sublists? Jul 31, 2015 at 23:21
• santosh, your last comment seems to ask a different question. Do you want elements common to any two sublists, or elements common to all sublists? I understood your original question to be the latter. Please clarify. Jul 31, 2015 at 23:39

Does this work:

Apply[
Intersection[#, SameTest -> (EuclideanDistance[#1, #2] < 10^(-5) &)] &,
Table[Table[Sqrt[p1]/r1, {r1, 0.1, 0.7, 0.1}], {p1, 0.5, 5, 0.5}]
]

(* gives: {1.01015, 1.17851, 1.41421, 1.76777, 2.35702, 3.53553, 7.07107} *)


You can probably adjust the numerical tolerance (10^-5) as needed.

• It gives first list {7.07107, 3.53553, 2.35702, 1.76777, 1.41421, 1.17851, 1.01015}. Please check Jul 31, 2015 at 21:45

I think you're after something like this:

ReplaceList[myListofLists, {a : ___, {b : ___, x_, ___}, c : ___, {d : ___, x_, ___}, ___} :>
{{Length@{a} + 1, Length@{b} + 1}, {Length@{a} + Length@{c} + 2, Length@{d} + 1}, x}]

(*
{{{1, 1}, {4, 2}, 7.07107}, {{1, 2}, {4, 4}, 3.53553},
{{1, 3}, {4, 6}, 2.35702}, {{2, 1}, {8, 2}, 10.},
{{2, 2}, {8, 4}, 5.}, {{2, 3}, {8, 6}, 3.33333}}
*)


Gives list with elements of {{Position of first list, position in that list},{position of second list, position in that list},number}... the coordinates can be converted to the values of p1 and r1 by multiplication by chosen increment and addition of chosen initial values for the table iterators.

If all you care about are the actual values,

Union @@ Intersection @@@ Subsets[myListofLists, {2}]


will suffice.