8
$\begingroup$

I have a list, for example:

data = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};

and I wanted to sum every element in range of five, i. e

ranges

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

and the result I want to get is:

{34, 38, 42, 46, 50}
$\endgroup$
8
  • 16
    $\begingroup$ Try Total[Partition[data, 5], {1}] $\endgroup$
    – Carl Woll
    Commented Jan 18, 2022 at 0:16
  • 6
    $\begingroup$ Or Total@GatherBy[data, Floor[(# - 1)/5] &] $\endgroup$
    – Bob Hanlon
    Commented Jan 18, 2022 at 0:47
  • 7
    $\begingroup$ Or Plus @@@ Transpose@Partition[data, 5]. $\endgroup$ Commented Jan 18, 2022 at 0:54
  • 5
    $\begingroup$ Oof. Lemme get a valid one in: Plus@@data[[#;;;;5]]&/@Range@5 $\endgroup$
    – Adam
    Commented Jan 18, 2022 at 1:07
  • 3
    $\begingroup$ {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20} // Partition[#,5]& // MapThread[Plus] $\endgroup$ Commented Apr 10, 2022 at 11:59

4 Answers 4

7
$\begingroup$

There are three ways that yield the desired outut: 1. Last@Accumulate@list Plus@@list and Total@list


Various approaches to get the list mentioned above


0. Internal`PartitionRagged

Last@Accumulate@
  Internal`PartitionRagged[data, Table[5, Length@data/5]]

also

Plus @@ Internal`PartitionRagged[data, Table[5, Length@data/5]]

and

Total@Internal`PartitionRagged[data, 
   Table[5, Length@data/5]]

1. ArrayReshape

Last@Accumulate@ArrayReshape[data, {Length@data/5, 5}]

also

Plus @@ ArrayReshape[data, {Length@data/5, 5}]

and

Total@ArrayReshape[data, {Length@data/5, 5}]

2. Fold + Partition + Flatten

Last@Accumulate@
  Fold[Partition, Flatten[data], {Length@data/5, 5}[[-1 ;; 2 ;; -1]]]

also

Plus @@ Fold[Partition, 
  Flatten[data], {Length@data/5, 5}[[-1 ;; 2 ;; -1]]]

and

Total@Fold[Partition, 
  Flatten[data], {Length@data/5, 5}[[-1 ;; 2 ;; -1]]]

3. TakeList

Last@Accumulate@TakeList[data, Table[5, Length@data/5]]

also

Plus @@ TakeList[data, Table[5, Length@data/5]]

and

Total@TakeList[data, Table[5, Length@data/5]]

4. FoldPairList + TakeDrop

Last@Accumulate@FoldPairList[TakeDrop, data, Table[5, Length@data/5]]

also

Plus @@ FoldPairList[TakeDrop, data, Table[5, Length@data/5]]

and

Total@FoldPairList[TakeDrop, data, Table[5, Length@data/5]]

All of the above and the solutions suggested in the comments section give

lisres

I will update the timings later tonight

$\endgroup$
3
$\begingroup$
data = Range[20];

Using MovingMap:

Total@MovingMap[# &, data, 4][[1 ;; -1 ;; 5]]

(*{34, 38, 42, 46, 50}*)

Or using BlockMap:

Total@BlockMap[# &, data, 5]

(*(*{34, 38, 42, 46, 50}*)*)
$\endgroup$
1
$\begingroup$
data = Range[20];

Using SequenceCases

Total @ SequenceCases[data, {Repeated[_, {5}]}]

{34, 38, 42, 46, 50}

Total @ SequenceCases[data, _?(Length[#] == 5 &)]

{34, 38, 42, 46, 50}

$\endgroup$
1
$\begingroup$

Using ArrayReduce and Partition:

data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
    19, 20};

Partition[data, 5] // ArrayReduce[Total, #, 1] &

Using MapThread:

MapThread[Plus@## &, Partition[data, 5]]

Result:

{34, 38, 42, 46, 50}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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