[Mathematica] Finding equal elements in a list

In summary, the conversation discusses finding the number of sublists in a nested list where the first two elements are equal and are followed by a sublist where the first element is higher than the second. Different methods are suggested, including using the Length and Cases functions, ReplaceList, and a Do loop.
  • #1
SoggyBottoms
59
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
  • #2
Length@Cases[{{1, 2, 3}, {1, 2, 4}, {1, 1, 2}}, {a_, a_, ___}]
 
  • #3
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?
 
  • #4
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}}
 
  • #5
Thanks a ton!
 

1. How do I find the number of equal elements in a list using Mathematica?

To find the number of equal elements in a list using Mathematica, you can use the Count function. For example, if you have a list named myList and you want to find the number of occurrences of the element "a", you can use Count[myList, "a"]. This will return the total number of times "a" appears in the list.

2. Can I use Mathematica to find the index of a specific element in a list?

Yes, you can use the Position function in Mathematica to find the index of a specific element in a list. For example, if you want to find the index of the element "b" in a list named myList, you can use Position[myList, "b"]. This will return a list of all the indices where "b" appears in myList.

3. How can I find all the equal elements in a list using Mathematica?

To find all the equal elements in a list using Mathematica, you can use the Gather function. This function will group together all the equal elements in the list. For example, if you have a list named myList and you want to find all the equal elements, you can use Gather[myList]. This will return a list of sublists, each containing all the equal elements.

4. Is it possible to find the most frequent element in a list using Mathematica?

Yes, you can use the Commonest function in Mathematica to find the most frequent element in a list. For example, if you have a list named myList and you want to find the most frequent element, you can use Commonest[myList]. This will return the element that appears the most number of times in the list.

5. Can Mathematica handle large lists when finding equal elements?

Yes, Mathematica can handle large lists when finding equal elements. It has efficient built-in functions such as Count, Position, and Gather that can handle large amounts of data. However, it is always recommended to use efficient coding practices to optimize the performance when dealing with large lists.

Similar threads

  • Programming and Computer Science
Replies
5
Views
856
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
261
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
22
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
549
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
Back
Top