Home > front end >  How do I output the result generated in if-loop as one array?
How do I output the result generated in if-loop as one array?

Time:12-29

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?

CodePudding user response:

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

  • Related