1
$\begingroup$

I have list given by:

  d=
    {{{1,2},{1,1},{2,3}},
    {{1,4},{3,3},{2,1}},
    {{1,1},{3,1},{3,3}}}

Each row in the list is a different generation of the list. I'm trying to figure out how to look at each iteration and see if any of the elements in each sublist are the same number, e.g. in generation "1" the middle sublist would fit this criteria since it's {1,1}. I have an If statement that finds out this information for a singular row:

m = 1;
tt=Table[
  If[
   d[[m, l]][[1]] == d[[m, l]][[2]],
   Print["Same"],
   Print["Different"]],
  {l, 1, Length[d[[1]]]}];

I would like to loop the "If" statement above so that it looks at all generations not just generation 1 in list "d." I then want to go through each table given by "tt" and find the number of times "Same" is printed, for each separate generation. Using Length[tt] doesn't distinguish between the two printed words so I'm not sure what control I need to add to have the number of only same printed. I've tried to use a For loop on the If statement to get it to perform the desired function but I know it's best to avoid usage of that. Any help would be appreciated.

$\endgroup$
4
  • 1
    $\begingroup$ This will count the number of pairs that are dupes: Count[#, v_?VectorQ /; Signature[v] == 0] & /@ {{{1, 2}, {1, 1}, {2, 3}}, {{1, 4}, {3, 3}, {2, 1}}, {{1, 1}, {3, 1}, {3, 3}}} Is this something you want? $\endgroup$ Commented Apr 21, 2020 at 1:00
  • $\begingroup$ Pretty cool use of Signature to check whether all the elements are the same! $\endgroup$
    – Victor K.
    Commented Apr 21, 2020 at 1:03
  • $\begingroup$ Yes that is what I'm looking for. Would it be possible to do the same thing for triplets? $\endgroup$
    – D'Angelo
    Commented Apr 21, 2020 at 1:16
  • $\begingroup$ Did you try it out already on triplets? It should work there, too. (OTOH, Signature[] will catch something like {1, 1, 2}, which might be what you don't want.) $\endgroup$ Commented Apr 21, 2020 at 1:21

2 Answers 2

2
$\begingroup$

You can count the number of tuples composed out of identical integers for each sublist with this expression:

Map[Count[#, {n_Integer ..}] &, d]

which yields for your example of d above

{1, 1, 2}
$\endgroup$
1
  • $\begingroup$ +1 but a little room for improvement, IMHO $\endgroup$
    – Mr.Wizard
    Commented May 23, 2020 at 20:11
1
$\begingroup$
d = {{{1, 2}, {1, 1}, {2, 3}}, {{1, 4}, {3, 3}, {2, 1}}, {{1, 1}, {3, 1}, {3, 3}}};

Count[{a_ ..}] /@ d
{1, 1, 2}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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