0

I have an array from which I only need value[1],value[4],value[7],value[10],value[13] and value[16] (so basically every third value after the skipping value[0]).

I need to do an equation with each of these values and return each answer into an array. The function I've written is, however, not making jumps of 3 and instead using every value. How would I fix this?

popt=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18] #I only want to use popt[1],popt[4],popt[7] etc. 

def d(popt):
  dvalues=[]
  i=1
  for i in range(0, len(popt)//3):
      d=wavelength()/(2 * math.sin(math.radians(popt[i] / 2)))
      dvalues.append(d)
      I+=3
  return(dvalues)
print(d(popt))
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43

2 Answers2

1

You need:

for i in range(1, len(popt), 3):
Carsten
  • 2,765
  • 1
  • 13
  • 28
0

Here is another approach which uses:

Sample Code:

import math

def d(popt):
    # I don't have your `wavelength()` function, so have used 1. 
    vals = [1 / (2 * math.sin(math.radians(i / 2))) for i in popt]
    return vals

# Create a list of every 3 numbers from 1-18. 
popt = [i for i in range(1, 18)][::3]
output = d(popt)

Output:

# Using 1, rather than your wavelength() function.
[57.296506740065155,
 14.326854173921912,
 8.19020411969913,
 5.736856622834928,
 4.416835735998784,
 3.5926482671638595]
S3DEV
  • 8,768
  • 3
  • 31
  • 42