6
$\begingroup$

Given a list

lst = {{{"1","2","3"},{"4","5"},{"6","7","8","9"}}, { "A","B","C","D","E","F","G","H","I"}, { "9","8","7","6","5","4","3","2","1"}}; 

How to get this by Partition.

res = {{{"1","2","3"},{"4","5"},{"6","7","8","9"}}, {{"A","B","C"},{"D","E"},{"F","G","H","I"}}, {{"9","8","7"},{"6","5"},{"4","3","2","1"}}}; 

Thanks!

$\endgroup$
0

7 Answers 7

10
$\begingroup$
lst2 = lst; lst2[[2 ;;]] = TakeList[Flatten @ #, Length /@ lst[[1]]] & /@ lst[[2 ;;]]; lst2 

{{{"1", "2", "3"}, {"4", "5"}, {"6", "7", "8", "9"}},
{{"A", "B", "C"}, {"D", "E"}, {"F", "G", "H", "I"}},
{{"9", "8", "7"}, {"6", "5"}, {"4", "3", "2", "1"}}}

You can also use the following to get the same result:

lst3 = lst; lst3[[2 ;;]] //= Map[TakeList[Flatten@#, Length /@ lst[[1]]] &]; lst3 lst4 = lst; lst4[[2 ;;]] = Function[x, Module[{k = 0}, Map[x[[k++]] &, lst[[1]], {-1}]]] /@ Rest[lst3]; lst4 MapAt[TakeList[Flatten @ #, Length /@ lst[[1]]] &, lst, {2;;}] Extract[Flatten @ #, List /@ Module[{k = 1}, Map[k++ &, lst[[1]], {-1}]]] & /@ lst 
$\endgroup$
1
  • $\begingroup$ Thanks! Master who is energetic and always online.;) $\endgroup$ Commented Jan 19, 2019 at 6:06
4
$\begingroup$
p = Length /@ First[lst]; res = Prepend[ Internal`PartitionRagged[#, p] & /@ Rest[lst], First[lst]] 
{{{1, 2, 3}, {4, 5}, {6, 7, 8, 9}}, {{A, B, C}, {D, E}, {F, G, H, I}}, {{9, 8, 7}, {6, 5}, {4, 3, 2, 1}}} 
$\endgroup$
4
$\begingroup$

The following also works. I removed the brackets

Prepend[Table[FoldPairList[TakeDrop, lst[[i]], Length /@ lst[[1]]], {i, 2, Length[lst]}], lst[[1]]] 

Also, many thanks to kglr for correcting my code.

FoldPairList[TakeDrop, #, Length /@ lst[[1]]] & /@ Rest[lst] 
$\endgroup$
2
  • 1
    $\begingroup$ Titus, re "Unfortunately, ..." , just move & outside FoldPairList[...], that is, use FoldPairList[TakeDrop, #, Length /@ lst[[1]]] & /@ Rest[lst]. $\endgroup$ Commented Jan 19, 2019 at 10:10
  • $\begingroup$ @kglr thank you very much! I do not handle pure functions well. $\endgroup$ Commented Jan 19, 2019 at 10:29
4
$\begingroup$

We can also use the (undocumented) built-in function Internal`CopyListStructure:

FoldList[Internal`CopyListStructure] @ lst 
{{{"1", "2", "3"}, {"4", "5"}, {"6", "7", "8", "9"}}, {{"A", "B", "C"}, {"D", "E"}, {"F", "G", "H", "I"}}, {{"9", "8", "7"}, {"6", "5"}, {"4", "3", "2", "1"}}} 

We can also use a combination of FoldList and TakeList to get the same result:

FoldList[TakeList[Flatten @ #2, Length /@ #] &] @ lst 
$\endgroup$
3
$\begingroup$
lst = {{{"1", "2", "3"}, {"4", "5"}, {"6", "7", "8", "9"}}, {"A", "B", "C", "D", "E", "F", "G", "H", "I"}, {"9", "8", "7", "6", "5", "4", "3", "2", "1"}}; 

Using Take and Table:

l = Flatten /@ lst; f = MinMax /@ ToExpression /@ First@# &; Table[Take[l[[i]], #] & /@ (f@#), {i, Length@#}] &@lst (*{{{"1", "2", "3"}, {"4", "5"}, {"6", "7", "8", "9"}}, {{"A", "B", "C"}, {"D", "E"}, {"F", "G", "H", "I"}}, {{"9", "8", "7"}, {"6", "5"}, {"4", "3", "2", "1"}}}*) 
$\endgroup$
2
$\begingroup$
a = {{{"1", "2", "3"}, {"4", "5"}, {"6", "7", "8", "9"}}, {"A", "B", "C", "D", "E", "F", "G", "H", "I"}, {"9", "8", "7", "6", "5", "4", "3", "2", "1"}}; cp = Internal`CopyListStructure; 

Using ReplaceAt (new in 13.1)

ReplaceAt[b_ :> cp[First @ a, b], {2 ;;}] @ a; MatrixForm[%] 

enter image description here

$\endgroup$
2
$\begingroup$
BlockMap[TakeList[#, Length /@ lst[[1]]] &, Flatten@lst, Length@lst[[2]]] (* { {{"1", "2", "3"}, {"4", "5"}, {"6", "7", "8", "9"}}, {{"A", "B", "C"}, {"D", "E"}, {"F", "G", "H", "I"}}, {{"9", "8", "7"}, {"6", "5"}, {"4", "3", "2", "1"}} } *) 
$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.