StefanE StefanE - 2 years ago 80
Python Question

Reshaping an dynamic array of numbers to a fixed size

I'm working with a small project where I'm to generate linear graphs and say for example I need to have 10 points but the data can be an array of 1000's of points.


Considering the array of integers (11 values) above and I want to the reshape this into an array with 3 values and at the same time adding up the values for a final result like this:

[14,49,18] (4 values + 4 values + 3 values)

What would the best approach be to have function that can handle any size (at least bigger than the size reshaping into) in python without any external libraries?

Answer Source

Did you want something like this:

a = [1, 5, 3, 5, 6, 33, 9, 1, 12, 4, 2]
step = 4
print [sum(a[i:i + step]) for i in range(0, len(a), step)]

which outputs

[14, 49, 18]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download