4
$\begingroup$

This question already has an answer here:

Background

If I want wo reshape list1 as list2:

list1 = {1, 2, 3, 4, 5, 6};
list2 = {{7, 4}, {1, 7}, {7, 6}};

We can use ArrayReshape and Dimensions to do it.

ArrayReshape[list1, Dimensions[list2]]

{{1, 2}, {3, 4}, {5, 6}}

Question

But when the list2 is a ragged list,how to do such thing with elegant method?

list = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
ragList = {{1, 2, 3}, {4, {5, 6}, 7}, {8}, {{{9}}, 10}};
$\endgroup$

marked as duplicate by WReach, MarcoB, corey979, Feyre, Mr.Wizard list-manipulation Feb 18 '17 at 13:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ Try using Flatten. $\endgroup$ – L.K. Feb 17 '17 at 21:49
  • $\begingroup$ @L.K. I mean how to make list to be ragList as its dimension $\endgroup$ – yode Feb 17 '17 at 21:51
  • $\begingroup$ @andre Do mind post it as an answer? $\endgroup$ – yode Feb 17 '17 at 22:22
  • $\begingroup$ I cast my close vote for the wrong question; I meant to mark this as a duplicate of (30405) $\endgroup$ – Mr.Wizard Feb 18 '17 at 13:12
  • $\begingroup$ @Mr.Wizard It's hard for me to find those relevant post.I'm very sad about this every time. $\endgroup$ – yode Feb 18 '17 at 13:30
6
$\begingroup$

You can do this :

list = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
ragList = {{a, b, c}, {d, {e, f}, g}, {h}, {{{i}}, j}};

i=1;
Map[list[[i++]]&,ragList,{-1}]
$\endgroup$
2
$\begingroup$

Assuming that the elements in ragList are unique, this would work as well:

ragList /. Map[#[[1]] -> #[[2]] &, Transpose[{Flatten[ragList], Flatten[list]}]]
(* {{1, 2, 3}, {4, {5, 6}, 7}, {8}, {{{9}}, 10}} *)
$\endgroup$
2
$\begingroup$
template = {{a, b, c}, {d, {e, f}, g}, {h}, {{{i}}, j}};
replist = Evaluate[(ReplacePart[template,
     Rule @@@ Transpose[{
        Reap[MapIndexed[Sow@#2 &, template, {-1}]][[2, 1]],
        Slot /@ Range@Length@Flatten@template}]] )] &

{{#1, #2, #3}, {#4, {#5, #6}, #7}, {#8}, {{{#9}}, #10}} &

list = Range@10;
replist@(Sequence @@ list)

{{1, 2, 3}, {4, {5, 6}, 7}, {8}, {{{9}}, 10}}

$\endgroup$
  • $\begingroup$ But how to get your ragList? $\endgroup$ – yode Feb 17 '17 at 22:32
  • $\begingroup$ On the other hand, if you will be doing this reshaping for many lists but the same ragList, then something along these lines might be much faster. $\endgroup$ – Carl Woll Feb 17 '17 at 22:43
2
$\begingroup$

Curent method:

reshape[list_, ragList_] := 
 Module[{pos = Position[ragList, _?NumericQ, Infinity]}, 
  ReplacePart[ragList, Thread[pos -> list]]]

Usage:

reshape[list, ragList]

{{1, 2, 3}, {4, {5, 6}, 7}, {8}, {{{9}}, 10}}

Performance

Function andre from this comment

andre[list_, ragList_] := 
 Module[{i = 1}, Map[list[[i++]] &, ragList, {-1}]]

MyPlot[{reshape @@ # &, 
  andre@@ # &}, {Range[#], 
   MapAt[List, Range[#], 
    List /@ RandomSample[Range[#], RandomInteger[#]]]} &, 11]

Ps:My MyPlot is MyPlot:=BenchmarkPlot[##1,IncludeFits->True]&

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.