1

Is it possible to systematically slice an 1d array of length m by an interval n in numpy? Say I have a list of 1000 values, could I break that into 10 lists of 100 values easily?

1
  • Look at np.split Commented Oct 12, 2020 at 5:02

2 Answers 2

2

You can use both np.array_split() and np.split() which in fact are the same with a little note (as per np.array_split())

From the documentation:

x = np.arange(8.0)

np.array_split(x, 3)

#Result
[array([0.,  1.,  2.]), array([3.,  4.,  5.]), array([6.,  7.])]

Split an array into multiple sub-arrays.

Please refer to the split documentation. The only difference between these functions is that array_split allows indices_or_sections to be an integer that does not equally divide the axis. For an array of length l that should be split into n sections, it returns l % n sub-arrays of size l//n + 1 and the rest of size l//n.

Sign up to request clarification or add additional context in comments.

Comments

1

array_split allows one to split with unequal spacing as well, should this ever meet your needs

ar = np.arange(0, 20, dtype='int')

s = [2, 7, 12, 17]

np.array_split(ar, s)
Out[80]: 
[array([0, 1]),
 array([2, 3, 4, 5, 6]),
 array([ 7,  8,  9, 10, 11]),
 array([12, 13, 14, 15, 16]),
 array([17, 18, 19])]

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.