Is there a quick method to transpose uneven lists without conditionals?
With:
Drop[Table[q, {10}], #] & /@ Range[10]
Thus the first list would have the first element of all the lists, the 2nd list would have all the 2nd elements of all the lists, etc. If there are no elements, skips. I have a feeling that this should incorporate Mathematica's Reap / Sow function, but unfamiliar.
Answer
Yes, but it is not trivial to comprehend. You would have to use the second argument of Flatten
to implement a generalized transpose of uneven lists. For example:
(* Uneven list *)
list = Range ~Array~ 5
Out[1]= {{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}, {1, 2, 3, 4, 5}}
(* Transpose the list *)
list ~Flatten~ {2}
Out[2]= {{1, 1, 1, 1, 1}, {2, 2, 2, 2}, {3, 3, 3}, {4, 4}, {5}}
For more information on how the second argument of Flatten
works and what it can do, see this answer by Leonid.
Comments
Post a Comment