# Rearrange triple sublists

Given a set of triple lists (all contain element 9!)

tripel = {{5, 10, 9}, {4, 9, 3}, {3, 9, 8}, {4, 5, 9}, {9, 10, 8}}


I would like to rearrange tripel to

Map[Which[#[[1]] == 9, #, #[[2]] ==9, {#[[2]], #[[3]], #[[1]]}, #[[3]] ==9, {#[[3]], #[[1]], #[[2]]}] &, tripel]
(*{{9, 5, 10}, {9, 3, 4}, {9, 8, 3}, {9, 4, 5}, {9, 10, 8}}*)


Is there a more direct and simple way to realize this cyclic permutation in Mathematica? Thanks!

• Just checking something: should the outcome be {{9, 5, 10}, {9, 4, 3}, {9, 3, 8}, {9, 4, 5}, {9, 10, 8}} or is it correct as it is?
– bmf
Nov 20, 2023 at 9:07
• @bmf I think it's correct as it is Nov 20, 2023 at 9:13

triples = {{5, 10, 9}, {4, 9, 3}, {3, 9, 8}, {4, 5, 9}, {9, 10, 8}}

NestWhile[RotateLeft, #, First@# =!= 9 &] & /@ triples


{{9, 5, 10}, {9, 3, 4}, {9, 8, 3}, {9, 4, 5}, {9, 10, 8}}

EDIT

The solution above works if there are more entries per sublist, but will run in a loop if 9 is not present. To improve, additional arguments are needed:

triples2 = {{5, 10, 9}, {4, 9, 3}, {3, 9, 8}, {4, 5, 9}, {4, 10,
8}, {3, 6, 8, 9, 10}}

NestWhile[RotateLeft, #, First@# =!= 9 &, 1, Length@#] & /@ triples2


{{9, 5, 10}, {9, 3, 4}, {9, 8, 3}, {9, 4, 5}, {4, 10, 8}, {9, 10, 3, 6, 8}}

• Thanks for your fast answer. It would be sufficient (in your edit) if only lists containing 9 would be returned! Nov 20, 2023 at 9:28
• I don't know of a way to do it with NestWhile unless an If is used. But then, it would be easier to pre-filter sublists that don't have a 9.
– Syed
Nov 20, 2023 at 9:31

Also using FirstPosition with the 3rd (default if no matching) parameter:

triples = {{5, 10, 9}, {4, 9, 3}, {3, 9, 8}, {4, 5, 9}, {9, 10, 8}, {1, 2, 3}};
RotateLeft[#, FirstPosition[#, 9, {1}] - 1] & /@ triples

(* {{9, 5, 10}, {9, 3, 4}, {9, 8, 3}, {9, 4, 5}, {9, 10, 8}, {1, 2, 3}} *)

• can save 4 characters removing [[1]] (+1, of course)
– kglr
Nov 20, 2023 at 10:16
• @kglr Changed it as suggested. Many thanks! :-) Nov 20, 2023 at 10:30
RotateLeft[#, PositionIndex[#] @ 9 - 1] & /@ tripel

{{9, 5, 10}, {9, 3, 4}, {9, 8, 3}, {9, 4, 5}, {9, 10, 8}}


To generalize to the case where 9 does not appear in some triples, replace PositionIndex[#] @ 9 with

Replace[_Missing -> 1] @ PositionIndex[#] @ 9

tripel = {{5, 10, 9}, {4, 9, 3}, {3, 9, 8}, {4, 5, 9}, {9, 10, 8}};

tripel /. {{a_, 9, c_} :> {9, c, a}, {a_, b_, 9} :> {9, a, b}}


{{9, 5, 10}, {9, 3, 4}, {9, 8, 3}, {9, 4, 5}, {9, 10, 8}}

• Thanks, that looks better readable than my Which- version Nov 20, 2023 at 9:21
 MapThread[RotateLeft,{#,Position[#,9][[All,2]]-1}]&[tripel]

(* {{9,5,10},{9,3,4},{9,8,3},{9,4,5},{9,10,8}} *)


Perhaps:

NestWhile[RotateLeft, #, #[[1]] != 9 &] & /@ tripel


yields: {{9, 5, 10}, {9, 3, 4}, {9, 8, 3}, {9, 4, 5}, {9, 10, 8}}

ReplaceRepeated[{a___, 9, b___} :> {9, b, a}] /@ tripel

{{9, 5, 10}, {9, 3, 4}, {9, 8, 3}, {9, 4, 5}, {9, 10, 8}}

list = {{5, 10, 9}, {4, 9, 3}, {3, 9, 8}, {4, 5, 9}, {9, 10, 8}};

Prepend[9] @* DeleteCases[9] /@ list


{{9, 5, 10}, {9, 4, 3}, {9, 3, 8}, {9, 4, 5}, {9, 10, 8}}

Update

As kglr commented the above doesn't give the expected result, which is

With[{p = Position[list, 9]},
MapAt[RotateRight, Cases[p, {a_, 3} :> {a}]] @
MapAt[RotateLeft, Cases[p, {a_, 2} :> {a}]] @ list]


{{9, 5, 10}, {9, 3, 4}, {9, 8, 3}, {9, 4, 5}, {9, 10, 8}}

• eldo, this does not give the desired result.
– kglr
May 11 at 9:00
• Thanks, kglr, I updated my answer
– eldo
May 11 at 9:26
Cases[tripel, {a___, 9, b___} :> {9, b, a}]

(* {{9, 5, 10}, {9, 3, 4}, {9, 8, 3}, {9, 4, 5}, {9, 10, 8}}  *)