I have this list,
{{a}, {i1, i2, i3}, {1, 2, 3}}
which I want to rearrange to this:
{{1, i1, a}, {2, i2, a}, {3, i3, a}}
It is for use with ListPlot3D[]. How can I do it?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityI have this list,
{{a}, {i1, i2, i3}, {1, 2, 3}}
which I want to rearrange to this:
{{1, i1, a}, {2, i2, a}, {3, i3, a}}
It is for use with ListPlot3D[]. How can I do it?
A crappy attempt:
{{a}, {i1, i2, i3}, {1, 2, 3}} /. {a_, b_, c_} :>
Thread[List[c, b, x]] /. x :> a
Thread[{#3,#2,Sequence@@#1}]&@@lst
{{1, i1, a}, {2, i2, a}, {3, i3, a}}
Slightly shorter
Thread[{#3,#2,First@#1}]&@@lst
list = {{a}, {i1, i2, i3}, {1, 2, 3}}
#~Join~First@list& /@Thread@Reverse@Rest@list
f = Thread @ Reverse @ PadRight[#, Automatic, #] &;
Examples:
lst1 = {{a}, {i1, i2, i3}, {1, 2, 3}};
f @ lst1
{{1, i1, a}, {2, i2, a}, {3, i3, a}}
lst2 = {{a}, {i1, i2, i3}, {x, y}, {1, 2, 3, 4}};
f @ lst2
{{1, x, i1, a}, {2, y, i2, a}, {3, x, i3, a}, {4, y, i1, a}}
Transpose
is a little more natural/accessible here than Thread
.
$\endgroup$
May 28, 2021 at 7:00
Transpose@Reverse[PadRight[#, 3, a] & /@ list]
$\endgroup$list={{a}, {i1, i2, i3, i4}, {1, 2, 3, 4}}
. $\endgroup$Transpose@ Reverse[PadRight[#, Length[list[[-1]]], list[[1, 1]]] & /@ list]
$\endgroup$ArrayPad[Thread@Reverse@Rest@list, {{0}, {0, 1}}, First@list]
$\endgroup$