Home > Software design >  how to split numpy array by step?
how to split numpy array by step?

Time:07-28

how to split numpy array by step?

Example:

I have array:

[3, 0, 5, 0, 7, 0, 3, 1]

I want to spit like this:

[3, 5, 6, 3]
[0, 0, 0, 1]

Or a more understandable example:

['a1', 'a2', 'b1', 'b2'] -- > ['a1', 'b1'] and ['a2', 'b2'] 

CodePudding user response:

You can do this with array slicing.

arr = np.array([3, 0, 5, 0, 7, 0, 3, 1])

A = arr[::2]
B = arr[1::2]

see docs on slices here

  • Related