List of lists of lists how to make a map recursively
Not very pretty but shorter:
foo // ClearAll
foo[list_List] := Module[{res}
, Sow @ MapIndexed[ If[ListQ @ #, Append[#2, Length @ #], #] &, list]
; foo /@ list;
]
Column @ Reap[foo@list][[-1, 1]]
{ab,ad,{3,4},aw} {be,br,pb,{4,8}} {cd,cr,ce,{4,1},{5,4},ne,{7,3},{8,4}} {fo} {gf,gh,gv,gg} {ks,kd,kl} {sa,sx,sr,{4,5}} {sdr,svd,{3,2},{4,3},ja} {ssfrt,styi} {sfc,sfb,smaa}
p.s. I don't like the fact that foo
needs to map list
twice but otherwise the order is wrong.
ClearAll[f0, tdl1, tdl2]
f0 = Replace[#, x_List :> Flatten[{Position[#, x], Length @ x}], 1] &;
You can use f0
with Extract
:
tdl1 = Extract[#, Sort@Position[#, _List], f0] &;
tdl1 @ list // Column // TeXForm
$\begin{array}{l} \{\text{ab},\text{ad},\{3,4\},\text{aw}\} \\ \{\text{be},\text{br},\text{pb},\{4,8\}\} \\ \{\text{cd},\text{cr},\text{ce},\{4,1\},\{5,4\},\text{ne},\{7,3\},\{8,4\}\} \\ \{\text{fo}\} \\ \{\text{gf},\text{gh},\text{gv},\text{gg}\} \\ \{\text{ks},\text{kd},\text{kl}\} \\ \{\text{sa},\text{sx},\text{sr},\{4,5\}\} \\ \{\text{sdr},\text{svd},\{3,2\},\{4,3\},\text{ja}\} \\ \{\text{ssfrt},\text{styi}\} \\ \{\text{sfc},\text{sfb},\text{smaa}\} \\ \end{array}$
or withTable
:
tdl2 = Table[f0[#[[## & @@ i]]], {i, Sort@Position[#, _List]}] &;
tdl1@list == tdl2@list
True