[Mathematica] Finding equal elements in a list

Click For Summary

Discussion Overview

The discussion revolves around finding specific patterns within nested lists in Mathematica, focusing on identifying sublists based on conditions related to their elements. The scope includes technical explanations and proposed solutions for programming challenges.

Discussion Character

  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant presents a nested list and asks how to find the number of sublists where the first two elements are equal.
  • Another participant suggests using the Mathematica function Length@Cases to achieve this.
  • A follow-up question is posed regarding a more complex condition where the first two elements must be equal and followed by a sublist where the first element is greater than the second.
  • Multiple methods are proposed to solve the second problem, highlighting different approaches such as using Partition, ReplaceList, and a loop with conditional checks.
  • Participants acknowledge the existence of multiple solutions, each with its own advantages and disadvantages regarding runtime, memory usage, and readability.

Areas of Agreement / Disagreement

Participants generally agree that there are multiple valid approaches to the problem, but no consensus is reached on a single best method.

Contextual Notes

Participants do not specify limitations or assumptions regarding the input data or the performance of the proposed methods.

SoggyBottoms
Messages
53
Reaction score
0
I have a nested list of the type:

{{1, 2, 3}, {1, 2, 4}, {1, 1, 2}}

I want to find the number of sublists in which the first two elements are equal, so in this case that's one (the sublist {1, 1, 2}).

Is this possible in Mathematica?
 
Physics news on Phys.org
Length@Cases[{{1, 2, 3}, {1, 2, 4}, {1, 1, 2}}, {a_, a_, ___}]
 
Thanks!

I want to do something a little more complicated now that I can't figure out either. I have a nested list again:

{{1, 2, 3}, {1, 2, 4}, {1, 1, 2}, {2, 1, 3}, {1, 1, 3}, {1, 2, 3}}

Now I want to find the number of sublists in which the first two elements are equal AND are followed by a sublist in which the first element is higher than the second.

So in this case I will get the same answer again, one (only the sublist {1, 1, 2} qualifies).

Is this possible as well?
 
As always, there are multiple ways. Each have advantages and disadvantages with respect to the others (run time, memory usage, readability etc..)

Code:
test = {{1, 2, 3}, {1, 2, 4}, {1, 1, 2}, {2, 1, 3}, {1, 1, 3}, {1, 2, 3}};

So, how about
Code:
Partition[test, 2, 1]
First /@ Cases[%, {{a_, a_, _}, {b_, c_, _}} /; b > c]

Out[2]= {{{1, 2, 3}, {1, 2, 4}}, {{1, 2, 4}, {1, 1, 2}}, {{1, 1, 2}, {2, 1, 3}}, {{2, 1, 3}, {1, 1, 3}}, {{1, 1, 3}, {1, 2, 3}}}
Out[3]= {{1, 1, 2}}

Or
Code:
ReplaceList[test, {___, A:{a_, a_, _}, {b_, c_, _}, ___}/;b>c :> A]

Out[4]= {{1, 1, 2}}

Or
Code:
out = {};
Do[If[test[[i, 1]] == test[[i, 2]] && test[[i + 1, 1]] > test[[i + 1, 2]], 
      PrependTo[out, i]], {i, 1, Length[test]}]
test[[out]]

Out[7]= {{1, 1, 2}}
 
Thanks a ton!
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K