I want to turn the list
list1 = {{a},{b,c},{d,e,f,g}}
into
{ {{1,a}}, {{2,b},{2,c}}, {{3,d}, {3,e}, {3,f}, {3,g}} }
How can I do this? I've tried different forms of Map but none is working.
I want to turn the list
list1 = {{a},{b,c},{d,e,f,g}}
into
{ {{1,a}}, {{2,b},{2,c}}, {{3,d}, {3,e}, {3,f}, {3,g}} }
How can I do this? I've tried different forms of Map but none is working.
MapIndexed[Thread[{#2[[1]], #}] &, list1]
{{{1, a}}, {{2, b}, {2, c}}, {{3, d}, {3, e}, {3, f}, {3, g}}}
Solutions by @b.gatessucks
MapThread[Thread[{#2, #1}] &, {list1, Range[Length[list1]]}]
(*or*)
Thread[{#[[1]], #[[2]]}] & /@ Transpose[{Range[Length[list1]], list1}]
I could not get @user11946's to work.
My own contribution is a generalisation.
The coordinates follow a geometric sequence: the first element is 1, the next two are 2, the next four are 4 etc. This can be tabulated as
list1 = {{a}, {b, c}, {d, e, f, g}}
bill = Table[
ConstantArray[i,
FoldList[Times, 1, Table[2, Length[list1]]][[i]]], {i, 1,
Length[list1]}]
In this thread I found many useful ways for element-wise Join in matrixes. I use one of them and define
threadJoin = Quiet[Re@##] /. Re -> List &;
threadJoin[bill, list1]
with the desired output. Most importantly, this is now marked as answered.
MapThread[Thread[{#2, #1}] &, {list1, Range[Length[list1]]}]orThread[{#[[1]], #[[2]]}] & /@ Transpose[{Range[Length[list1]], list1}? – b.gates.you.know.what Nov 21 '18 at 16:38