1
$\begingroup$

I'm trying to classify a list according to its sorted sublists.

For example:

list = {{1, 1, 1}, {2, 1, 1}, {2, 2, 1}, {3, 2, 1}};
e1 = Table[{}, {i, 1, Length[list]}];
e2 = Table[{}, {i, 1, Length[list]}];
e3 = Table[{}, {i, 1, Length[list]}];
e4 = Table[{}, {i, 1, Length[list]}];
For[j = 1, j < Length[list] + 1, j++, tmp1 = Flatten@list[[j]];
  Print[tmp1]
   If[Length[Union[tmp1]] == 1, e1[[j]] = tmp1];
  If[Length[Union[tmp1]] == 3, e4[[j]] = tmp1];];

In this manner I can classify the sublists of {1,1,1} and {3,2,1}, however it is not sufficient to classify {1,1,2} and {2,2,1}, how can both sublists be distinguished?

The criterion I wish to have is when the larger element in the sublist is larger or smaller than the other two.

$\endgroup$

1 Answer 1

1
$\begingroup$

Something like this?

list = {{1, 1, 1}, {2, 1, 1}, {2, 2, 1}, {3, 2, 1}};

g = GroupBy[list, {CountDistinct[#], Median[#] > Min[#]} &]
(*    <|{1, False} -> {{1, 1, 1}},
        {2, False} -> {{2, 1, 1}},
        {2, True}  -> {{2, 2, 1}},
        {3, True}  -> {{3, 2, 1}}|>    *)

{e1, e2, e3, e4} = Lookup[g, {{1, False}, {2, False}, {2, True}, {3, True}}, {}];

e1
(*    {{1, 1, 1}}    *)

e2
(*    {{2, 1, 1}}    *)

e3
(*    {{2, 2, 1}}    *)

e4
(*    {{3, 2, 1}}    *)
$\endgroup$
2
  • $\begingroup$ it does the job, however I wish to use the classification to separate the values into separate lists. some of the lists hold all the criterions (like this example) and some not. In short, if the list were list={{{1,1,1},{2,1,1},{2,2,1},{3,2,1}},{{1,1,1},{2,1,1}}}, how would you automatically classify the sublists and move them into e1, e2, e3 etc..? $\endgroup$
    – jarhead
    May 2, 2021 at 16:25
  • $\begingroup$ see updated answer for assignment to the e1,e2,e3,e4 variables. $\endgroup$
    – Roman
    May 2, 2021 at 16:35

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.