I am trying to split an array into n parts. Sometimes these parts are of the same size, sometimes they are of a different size.
I am trying to use:
JavaScript
x
2
1
split = np.split(list, size)
2
This works fine when size divides equally into the list, but fails otherwise. Is there a way to do this which will ‘pad’ the final array with the extra ‘few’ elements?
Advertisement
Answer
JavaScript
1
4
1
def split_padded(a,n):
2
padding = (-len(a))%n
3
return np.split(np.concatenate((a,np.zeros(padding))),n)
4