TransWikia.com

How can I rearrange my list?

Mathematica Asked on June 4, 2021

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?

4 Answers

A crappy attempt:

{{a}, {i1, i2, i3}, {1, 2, 3}} /. {a_, b_, c_} :> 
   Thread[List[c, b, x]] /. x :> a

Correct answer by Charmbracelet on June 4, 2021

list = {{a}, {i1, i2, i3}, {1, 2, 3}} 
#~Join~First@list& /@Thread@Reverse@Rest@list

Answered by theorist on June 4, 2021

Thread[{#3,#2,Sequence@@#1}]&@@lst

{{1, i1, a}, {2, i2, a}, {3, i3, a}}

Slightly shorter

Thread[{#3,#2,First@#1}]&@@lst

Answered by user1066 on June 4, 2021

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}}

Answered by kglr on June 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP