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?

Limona2000
  • 37
  • 6
  • have you tried [`numpy.ndarray.flatten`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.flatten.html)? – joshmeranda Dec 28 '21 at 21:01
  • @joshmeranda Sorry, I should edit my post: I get a collection of single integer/float objects, not arrays. – Limona2000 Dec 28 '21 at 21:02

1 Answers1

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

Constantin Guidon
  • 1,892
  • 16
  • 21