Since I cannot explain Kuba's elegant solution here is a step for step approach to your problem:
list = {{a, {1, 2, 3}}, {b, {1, 2, 4}}, {c, {{1, 4, 5, 5}, {6, 3, 2, 1}, {7, 4, 5}}}};
Find the position of the last symbol (c
):
p = First@Last@Position[list, s_Symbol /; s =!= List]
3
(Since List
is a Symbol
I excluded it in the above statement).
Now get the matrix prepended by the last symbol:
q = list[[p, 2]]
{{1, 4, 5, 5}, {6, 3, 2, 1}, {7, 4, 5}}
Prepend q
with c1
... ci
:
tab = Table[{ToExpression["c" <> ToString[i]], q[[i]]}, {i, 1, Length@q}]
{{c1, {1, 4, 5, 5}}, {c2, {6, 3, 2, 1}}, {c3, {7, 4, 5}}}
Find the positions of entries which already had symbols:
h = Most@Range@p
{1, 2}
list[[h]]~Join~tab
{{a, {1, 2, 3}}, {b, {1, 2, 4}}, {c1, {1, 4, 5, 5}}, {c2, {6, 3, 2, 1}}, {c3, {7, 4, 5}}}
Put everything together:
fun[list_] :=
With[{p = First@Last@Position[list, s_Symbol /; s =!= List]},
list[[Most@Range@p]]~Join~
Table[{ToExpression["c" <> ToString[i]], #[[i]]}, {i, 1,
Length@#}] &[list[[p, 2]]]]
fun@list
{{a, {1, 2, 3}}, {b, {1, 2, 4}}, {c1, {1, 4, 5, 5}}, {c2, {6, 3, 2, 1}}, {c3, {7, 4, 5}}}
list /. {s_, x : {__List}} :> (## & @@ Table[{Symbol[SymbolName[s] <> ToString[i]], x[[ i]]}, {i, Length@x}])
$\endgroup$c
and would like to end up withc1
andc2
? $\endgroup$"I want to import it to CSV"
? $\endgroup$