Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sums of variable size chunks of a list where sizes are given by other list

Tags:

python

numpy

I would like to make the following sum given two lists:

a = [0,1,2,3,4,5,6,7,8,9]
b = [2,3,5]

The result should be the sum of the every b element of a like:

  1. b[0] = 2 so the first sum result should be: sum(a[0:2])
  2. b[1] = 3 so the second sum result should be: sum(a[2:5])
  3. b[2] = 5 so the third sum result should be: sum(a[5:10])

The printed result: 1,9,35

like image 709
Sady Bogarin Avatar asked May 07 '20 13:05

Sady Bogarin


People also ask

How to get chunk size of a list in Python?

Using a for loop and range () method, iterate from 0 to the length of the list with the size of chunk as the step. Return the chunks using yield. list_a [i:i+chunk_size] gives each chunk. For example, when i = 0, the items included in the chunk are i to i + chunk_size which is 0 to (0 + 2)th index.

How to sort an array by the largest number of chunks?

We split arr into some number of chunks (i.e., partitions), and individually sort each chunk. After concatenating them, the result should equal the sorted array. Return the largest number of chunks we can make to sort the array. Input: arr = [4,3,2,1,0] Output: 1 Explanation: Splitting into two or more chunks will not return the required result.

What is the maximum number of chunks that can be split?

Input: arr = [1,0,2,3,4] Output: 4 Explanation: We can split into two chunks, such as [1, 0], [2, 3, 4]. However, splitting into [1, 0], [2], [3], [4] is the highest number of chunks possible.

How to find the sum of elements in a list?

In the while loop, we will access each element in the list using the count variable and add them to sumOfElements. After that, we will increment the value of the count by 1. We will continue this process until the count becomes equal to the length of the list. You can write a program to find the sum of elements in a list in python as follows.


4 Answers

You can make use of np.bincount with weights:

groups = np.repeat(np.arange(len(b)), b)

np.bincount(groups, weights=a)

Output:

array([ 1.,  9., 35.])
like image 91
Quang Hoang Avatar answered Nov 15 '22 21:11

Quang Hoang


NumPy has a tool to do slice based sum-reduction with np.add.reduceat -

In [46]: np.add.reduceat(a,np.cumsum(np.r_[0,b[:-1]]))                          
Out[46]: array([ 1,  9, 35])
like image 31
Divakar Avatar answered Nov 15 '22 22:11

Divakar


Hard to compete with the np.bincount solution, but here's another nice way to approach it with np.cumsum:

strides = [0] + np.cumsum(b).tolist()  # [0, 2, 5, 10]
stride_slices = zip(strides[:-1], strides[1:])  # [(0, 2), (2, 5), (5, 10)]
[sum(a[s[0]: s[1]]) for s in stride_slices]
# [1, 9, 35]
like image 31
TayTay Avatar answered Nov 15 '22 20:11

TayTay


You mean something like this?

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [2, 3, 5]

def some_function(a, b): # couldnt come up with a name :D
    last_index = 0

    for i in b:
        print(sum(a[last_index:last_index + i]))
        last_index += i

some_function(a, b)
like image 41
muhmann Avatar answered Nov 15 '22 21:11

muhmann