Mathematica Asked on June 23, 2021
Consider the following list, with sublists of different lengths
list = {{{0.8, 0.8, 0.8}}, {{1, 1, 1}, {4, 1, 1}, {3, 3, 2}}, {{1.2,
1.2, 1.2}}, {{1.4, 1.4, 1.4}}, {{1.6, 1.6, 1.6}, {4, 2, 2}}};
Map[Length, list]; (*{1, 3, 1, 1, 2}*)
n = Max[%]; (*3*)
how can this this list be separated into n
lists of the length of the original list, where the positions of the elements are maintained, i.e, the output should be
e1={{{0.8, 0.8, 0.8}}, {{1, 1, 1}}, {{1.2, 1.2, 1.2}}, {{1.4,1.4,1.4}}, {{1.6, 1.6, 1.6}}}
e2={{}, {{4, 1, 1}}, {}, {}, {{4, 2, 2}}}
e3={{}, {{3, 3, 2}}, {}, {}, {}}
Transpose @ PadRight[list, Automatic, {}]
{{{0.8, 0.8, 0.8}, {1, 1, 1}, {1.2, 1.2, 1.2}, {1.4, 1.4, 1.4}, {1.6, 1.6, 1.6}}, {{}, {4, 1, 1}, {}, {}, {4, 2, 2}}, {{}, {3, 3, 2}, {}, {}, {}}}
Transpose[PadRight[list, Automatic, {}]] /. p : {__?NumericQ} :> {p}
{{{{0.8, 0.8, 0.8}}, {{1, 1, 1}}, {{1.2, 1.2, 1.2}}, {{1.4, 1.4, 1.4}}, {{1.6, 1.6, 1.6}}}, {{}, {{4, 1, 1}}, {}, {}, {{4, 2, 2}}}, {{}, {{3, 3, 2}}, {}, {}, {}}}
Correct answer by kglr on June 23, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP