3
$\begingroup$

Given a list of the form:

{{1,2,3,4,5},{6,7,8,9,10},{11,12},{13,14,15,16},{17,18,19,20,21,22,23},{24},{25,26},{27},{4}}

How can I bin each sequential set of N elements of the list together? For example, if N = 3, we would transform the above list into:

{{1,2,3,4,5,6,7,8,9,10,11,12},{13,14,15,16,17,18,19,20,21,22,23,24},{25,26,27,4}}

Of length 3.

$\endgroup$
  • 1
    $\begingroup$ You could use Partition and Join. $\endgroup$ – b.gates.you.know.what Sep 13 '13 at 11:22
  • $\begingroup$ @b.gatessucks So apply Partition with the desired value of N, and then flatten each of the new elements? $\endgroup$ – HStoley Sep 13 '13 at 11:24
  • $\begingroup$ e.g. like so: Join @@@ Partition[list, 3], but depends a lot on what you plan to do $\endgroup$ – Pinguin Dirk Sep 13 '13 at 11:25
  • $\begingroup$ @b.gatessucks Is there an easy command to apply Flatten[ ,1] to each element in the list? $\endgroup$ – HStoley Sep 13 '13 at 11:25
  • $\begingroup$ @PinguinDirk Ah, I see, that makes sense. $\endgroup$ – HStoley Sep 13 '13 at 11:25
6
$\begingroup$

Let

list={{1,2,3,4,5},{6,7,8,9,10},{11,12},{13,14,15,16},
       {17,18,19,20,21,22,23},{24},{25,26},{27},{4}}

Thanks to @b.gatessucks, you could either:

 Join @@@ Partition[list, 3]

or

 Flatten /@ Partition[list, 3]

both return:

{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, {13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}, {25, 26, 27, 4}}

Note that depending on the initial structure of list, maybe "unexpected" results might occur, e.g. try the following (dropping the last element, i.e. {4}):

Flatten /@ Partition[Most@list, 3]

{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, {13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}}

Why? Most@listnow only has 8 elements, thus it is hard to partition in groups of three. By standard, Partition only returns the "full" (i.e. consisting of 3 elements) lists.

To react, you could e.g. use:

Flatten /@ Partition[Most@list, 3, 3, 1, {}]

{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, {13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}, {25, 26, 27}}

See the documentation of Partition for more information.

Also, check out the dynP function by Mr.Wizard!

$\endgroup$
4
$\begingroup$

Try!

Partition[Flatten@#1, #2, #2, 1, {}] & @@ {list, 12}
$\endgroup$
2
$\begingroup$
Partition[list, 3, 3, 1, {}, Join]

{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
{13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24},
{25, 26, 27, 4}}

Also

BlockMap[Flatten, list,3]

{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
{13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24},
{25, 26, 27, 4}}

$\endgroup$
  • $\begingroup$ Thanks for bringing BlockMap to my attention. (+1) $\endgroup$ – Edmund Jan 6 '18 at 13:43
  • $\begingroup$ @Edmund, thank you for the upvote. $\endgroup$ – kglr Jan 6 '18 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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