1

I have a numpy array and I want to extract every 3rd rows from it

input

0.00    1.0000 
0.34    1.0000 
0.68    1.0000 
1.01    1.0000
1.35    1.0000
5.62    2.0000

I need to extract every 3rd row so that expected output will be

0.68    1.0000 
5.62    2.0000

My code:

import numpy as np
a=np.loadtxt('input.txt')
out=a[::3]

But it gives different result.Hope experts will guide me.Thanks.

1 Answer 1

2

When undefined, the starting point of a (positive) slice is the first item.

You need to slice starting on the n-1th item:

N = 3
out = a[N-1::N]

Output:

array([[0.68, 1.  ],
       [5.62, 2.  ]])
Sign up to request clarification or add additional context in comments.

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.