2

I have my data as an array, and I want to extract every 7th value in the data. My code is like this:

import numpy as np

n=np.arange(0,100,1)
data=np.array(np.arange(0,100,1))

for n in n:
    if n%7==0:
        array=data[n]

But instead of one array, the 'array' in this output is actually a collection of single int objects(I would like to do this for float objects as well) that were looped over. How can I output one array or is if-loop not the way to do it?

2
  • have you tried numpy.ndarray.flatten? Commented Dec 28, 2021 at 21:01
  • @joshmeranda Sorry, I should edit my post: I get a collection of single integer/float objects, not arrays. Commented Dec 28, 2021 at 21:02

1 Answer 1

1

You can simply do this:

import numpy as np

n=np.arange(0,100,1)
data=np.array(np.arange(0,100,1))
data[::7]

for more informations: Understanding slice notation

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

4 Comments

Yes! That's exactly what I'm looking for. Thanks!
The slice notation also seems very useful. I'll have a good read for sure.
@Limona2000 if the answer is exactly what you need, consider accepting it ;)
@mozway Good call, just didd! Sorry, just started using this :)

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.