Skip to main content
added 272 characters in body
Source Link

(it doesn't really matter what f doesit doesn't really matter what f does*) however, when I try to do this with a one-liner I don't get what I expect,

{{{1, 2, {3, 4}, 5, 6, 7, 8},
  {1, 2, 3, {4, 5}, 6, 7, 8},
  {1, 2, 3, 4, {5, 6}, 7, 8}}}

I didn't think the particular definition of f would matter, but since it is mentioned in the comments, this is it:

f[L_] := Table[
   Delete[ReplacePart[L, p+1 -> {L[[p+1]], L[[p+2]]}], p+2]
   , {p, 2, Length[L]-4}] /; Length[L] >= 6

(it doesn't really matter what f does) however, when I try to do this with a one-liner I don't get what I expect,

{{{1, 2, {3, 4}, 5, 6, 7, 8},
  {1, 2, 3, {4, 5}, 6, 7, 8},
  {1, 2, 3, 4, {5, 6}, 7, 8}}}

(it doesn't really matter what f does*) however, when I try to do this with a one-liner I don't get what I expect,

{{{1, 2, {3, 4}, 5, 6, 7, 8},
  {1, 2, 3, {4, 5}, 6, 7, 8},
  {1, 2, 3, 4, {5, 6}, 7, 8}}}

I didn't think the particular definition of f would matter, but since it is mentioned in the comments, this is it:

f[L_] := Table[
   Delete[ReplacePart[L, p+1 -> {L[[p+1]], L[[p+2]]}], p+2]
   , {p, 2, Length[L]-4}] /; Length[L] >= 6
Source Link

How to combine Nest and list Part

I have a list $L$ of Length $n\geq6$ and a function $f$ that, when applied to $L$, generates $n-5$ lists $\mathscr{L}_i$ of Length $n-1$ (at the outermost level). I want to then apply $f$ to each $\mathscr{L}_i$, then repeat with the generated lists and keep doing that until I get $(n-5)!$ lists of length 5 (which I suppose is right).

I only wanna do this for small $n$, so performance is not a problem, I just can't figure out the way to do it: I've been trying Nest, NestWhile and the List versions together with Table to access the generated list elements but to no success.

To show you exactly what I'm doing, for $n=8$, I have e.g.

f[Range[8]]
Flatten[Table[f[%[[p]]], {p, Length[%]}], 1]
Flatten[Table[f[%[[p]]], {p, Length[%]}], 1]

for which the output is

{{1, 2, {3, 4}, 5, 6, 7, 8},
 {1, 2, 3, {4, 5}, 6, 7, 8},
 {1, 2, 3, 4, {5, 6}, 7, 8}}

{{1, 2, {{3, 4}, 5}, 6, 7, 8},
 {1, 2, {3, 4}, {5, 6}, 7, 8},
 {1, 2, {3, {4, 5}}, 6, 7, 8},
 {1, 2, 3, {{4, 5}, 6}, 7, 8},
 {1, 2, {3, 4}, {5, 6}, 7, 8},
 {1, 2, 3, {4, {5, 6}}, 7, 8}}

{{1, 2, {{{3, 4}, 5}, 6}, 7, 8},
 {1, 2, {{3, 4}, {5, 6}}, 7, 8},
 {1, 2, {{3, {4, 5}}, 6}, 7, 8},
 {1, 2, {3, {{4, 5}, 6}}, 7, 8},
 {1, 2, {{3, 4}, {5, 6}}, 7, 8},
 {1, 2, {3, {4, {5, 6}}}, 7, 8}}

(it doesn't really matter what f does) however, when I try to do this with a one-liner I don't get what I expect,

NestWhileList[
 Flatten[Table[f[# &[[p]]], {p, Length[# &]}], 1], 
 f[Range[8]], Length[#] & < 3!]

just gives

{{{1, 2, {3, 4}, 5, 6, 7, 8},
  {1, 2, 3, {4, 5}, 6, 7, 8},
  {1, 2, 3, 4, {5, 6}, 7, 8}}}