# position of intersection of two lists

I have the two lists:

s1 ={0.,0.00333333,0.00666667,0.01,0.0133333,0.0166667,0.02,0.0233333,0.0266667,0.03,0.0333333,0.0366667,0.04,0.0433333,0.0466667,0.05,0.0533333,0.0566667,0.06,0.0633333,0.0666667,0.07,0.0733333,0.0766667,0.08,0.0833333,0.0866667,0.09,0.0933333,0.0966667,0.1,0.103333,0.106667,0.11,0.113333,0.116667,0.12,0.123333,0.126667,0.13,0.133333,0.136667,0.14,0.143333,0.146667,0.15,0.153333,0.156667,0.16,0.163333,0.166667,0.17,0.173333,0.176667,0.18,0.183333,0.186667,0.19,0.193333,0.196667,0.2,0.203333,0.206667,0.21,0.213333,0.216667,0.22,0.223333,0.226667,0.23,0.233333,0.236667,0.24,0.243333,0.246667,0.25,0.253333,0.256667,0.26,0.263333,0.266667,0.27,0.273333,0.276667,0.28,0.283333,0.286667,0.29,0.293333,0.296667,0.3,0.303333,0.306667,0.31,0.313333,0.316667,0.32,0.323333,0.326667,0.33,0.333333,0.336667,0.34,0.343333,0.346667,0.35,0.353333,0.356667,0.36,0.363333,0.366667,0.37,0.373333,0.376667,0.38,0.383333,0.386667,0.39,0.393333,0.396667,0.4,0.403333,0.406667,0.41,0.413333,0.416667,0.42,0.423333,0.426667,0.43,0.433333,0.436667,0.44,0.443333,0.446667,0.45,0.453333,0.456667,0.46,0.463333,0.466667,0.47,0.473333,0.476667,0.48,0.483333,0.486667,0.49,0.493333,0.496667,0.5,0.503333,0.506667,0.51,0.513333,0.516667,0.52,0.523333,0.526667,0.53,0.533333,0.536667,0.54,0.543333,0.546667,0.55,0.553333,0.556667,0.56,0.563333,0.566667,0.57,0.573333,0.576667,0.58,0.583333,0.586667,0.59,0.593333,0.596667,0.6,0.603333,0.606667,0.61,0.613333,0.616667,0.62,0.623333,0.626667,0.63,0.633333,0.636667,0.64,0.643333,0.646667,0.65,0.653333,0.656667,0.66,0.663333};


and

s2={0.,0.00222222,0.00444444,0.00666667,0.00888889,0.0111111,0.0133333,0.0155556,0.0177778,0.02,0.0222222,0.0244444,0.0266667,0.0288889,0.0311111,0.0333333,0.0355556,0.0377778,0.04,0.0422222,0.0444444,0.0466667,0.0488889,0.0511111,0.0533333,0.0555556,0.0577778,0.06,0.0622222,0.0644444,0.0666667,0.0688889,0.0711111,0.0733333,0.0755556,0.0777778,0.08,0.0822222,0.0844444,0.0866667,0.0888889,0.0911111,0.0933333,0.0955556,0.0977778,0.1,0.102222,0.104444,0.106667,0.108889,0.111111,0.113333,0.115556,0.117778,0.12,0.122222,0.124444,0.126667,0.128889,0.131111,0.133333,0.135556,0.137778,0.14,0.142222,0.144444,0.146667,0.148889,0.151111,0.153333,0.155556,0.157778,0.16,0.162222,0.164444,0.166667,0.168889,0.171111,0.173333,0.175556,0.177778,0.18,0.182222,0.184444,0.186667,0.188889,0.191111,0.193333,0.195556,0.197778,0.2,0.202222,0.204444,0.206667,0.208889,0.211111,0.213333,0.215556,0.217778,0.22,0.222222,0.224444,0.226667,0.228889,0.231111,0.233333,0.235556,0.237778,0.24,0.242222,0.244444,0.246667,0.248889,0.251111,0.253333,0.255556,0.257778,0.26,0.262222,0.264444,0.266667,0.268889,0.271111,0.273333,0.275556,0.277778,0.28,0.282222,0.284444,0.286667,0.288889,0.291111,0.293333,0.295556,0.297778,0.3,0.302222,0.304444,0.306667,0.308889,0.311111,0.313333,0.315556,0.317778,0.32,0.322222,0.324444,0.326667,0.328889,0.331111,0.333333,0.335556,0.337778,0.34,0.342222,0.344444,0.346667,0.348889,0.351111,0.353333,0.355556,0.357778,0.36,0.362222,0.364444,0.366667,0.368889,0.371111,0.373333,0.375556,0.377778,0.38,0.382222,0.384444,0.386667,0.388889,0.391111,0.393333,0.395556,0.397778,0.4,0.402222,0.404444,0.406667,0.408889,0.411111,0.413333,0.415556,0.417778,0.42,0.422222,0.424444,0.426667,0.428889,0.431111,0.433333,0.435556,0.437778,0.44,0.442222};


I want to find the intersection of the two lists and the position of the elements of intersection in each list, something like this:

{{p1 (element of intersection), g1 (position of p1 in s1), h1 (position of p1 in s2), {.....}, {......}......} Thanks for help.

• Can s1 and s2 have repeated entries? – J. M.'s discontentment Mar 22 '18 at 1:53
• @J.M. NO. Elements are distinct. – qahtah Mar 22 '18 at 2:01

{#, Position[s1, #][[1, 1]], Position[s2, #][[1, 1]]} & /@ Intersection[s1, s2]


This finds the set of elements in the Intersection, and then makes a list with 1) each such element, 2) that element's location in list s1, and 3) that element's location in s2.

If there are repeated entries of an intersection element, use this:

{#, Position[s1, #], Position[s2, #]} & /@ Intersection[s1, s2]

• Perfect. Many thanks – qahtah Mar 22 '18 at 1:56

Using a modification of @Szabolcs's positionDuplicates in this Q/A

ClearAll[intersectionAndPositions]
intersectionAndPositions = Module[{l = Length@#, s = Join[##], g},
g = Mod[Select[GatherBy[Range[Length@s], s[[#]] &], Length@# >= 2 &], l, 1];
{s[[#[[1]]]], #} & /@ g] &;

intersectionAndPositions [s1, s2] // Grid[#, Dividers -> All] & // TeXForm


$$\begin{array}{|c|c|} \hline 0. & \{1,1\} \\ \hline 0.00666667 & \{3,4\} \\ \hline 0.0133333 & \{5,7\} \\ \hline 0.02 & \{7,10\} \\ \hline 0.0266667 & \{9,13\} \\ \hline 0.0333333 & \{11,16\} \\ \hline 0.04 & \{13,19\} \\ \hline 0.0466667 & \{15,22\} \\ \hline 0.0533333 & \{17,25\} \\ \hline 0.06 & \{19,28\} \\ \hline 0.0666667 & \{21,31\} \\ \hline 0.0733333 & \{23,34\} \\ \hline 0.08 & \{25,37\} \\ \hline 0.0866667 & \{27,40\} \\ \hline 0.0933333 & \{29,43\} \\ \hline 0.1 & \{31,46\} \\ \hline 0.106667 & \{33,49\} \\ \hline 0.113333 & \{35,52\} \\ \hline 0.12 & \{37,55\} \\ \hline 0.126667 & \{39,58\} \\ \hline 0.133333 & \{41,61\} \\ \hline 0.14 & \{43,64\} \\ \hline 0.146667 & \{45,67\} \\ \hline 0.153333 & \{47,70\} \\ \hline 0.16 & \{49,73\} \\ \hline 0.166667 & \{51,76\} \\ \hline 0.173333 & \{53,79\} \\ \hline 0.18 & \{55,82\} \\ \hline 0.186667 & \{57,85\} \\ \hline 0.193333 & \{59,88\} \\ \hline 0.2 & \{61,91\} \\ \hline 0.206667 & \{63,94\} \\ \hline 0.213333 & \{65,97\} \\ \hline 0.22 & \{67,100\} \\ \hline 0.226667 & \{69,103\} \\ \hline 0.233333 & \{71,106\} \\ \hline 0.24 & \{73,109\} \\ \hline 0.246667 & \{75,112\} \\ \hline 0.253333 & \{77,115\} \\ \hline 0.26 & \{79,118\} \\ \hline 0.266667 & \{81,121\} \\ \hline 0.273333 & \{83,124\} \\ \hline 0.28 & \{85,127\} \\ \hline 0.286667 & \{87,130\} \\ \hline 0.293333 & \{89,133\} \\ \hline 0.3 & \{91,136\} \\ \hline 0.306667 & \{93,139\} \\ \hline 0.313333 & \{95,142\} \\ \hline 0.32 & \{97,145\} \\ \hline 0.326667 & \{99,148\} \\ \hline 0.333333 & \{101,151\} \\ \hline 0.34 & \{103,154\} \\ \hline 0.346667 & \{105,157\} \\ \hline 0.353333 & \{107,160\} \\ \hline 0.36 & \{109,163\} \\ \hline 0.366667 & \{111,166\} \\ \hline 0.373333 & \{113,169\} \\ \hline 0.38 & \{115,172\} \\ \hline 0.386667 & \{117,175\} \\ \hline 0.393333 & \{119,178\} \\ \hline 0.4 & \{121,181\} \\ \hline 0.406667 & \{123,184\} \\ \hline 0.413333 & \{125,187\} \\ \hline 0.42 & \{127,190\} \\ \hline 0.426667 & \{129,193\} \\ \hline 0.433333 & \{131,196\} \\ \hline 0.44 & \{133,199\} \\ \hline \end{array}$$

• Excellent (+1) but this code gives ambiguous output when a list contains repeated entries in the intersection. (Admittedly, the OP precludes such cases.) – David G. Stork Mar 22 '18 at 2:14
• @kglr Thanks for your input. – qahtah Mar 22 '18 at 2:19
• @DavidG.Stork, thank you for the vote. For the repeated entries case, you can SplitBy the index set before applying Mod. – kglr Mar 22 '18 at 2:26
• @qahtah, my pleasure. – kglr Mar 22 '18 at 2:27