Speed up python loop
for-loop, performance, python
Solution
You could probably try:
mag_list = [value for value in var_s[4::3] if value != 99.]
depending on `var_s`, you might do better using `itertools.islice(var_s,4,None,3)`, but that would definitely need to be timed to know.
Perhaps you'd do even better if you stuck with numpy the whole way:
vs = np.array(var_s[4::3],dtype=np.float64) #could slice after array conversion too ...
med_mag = np.median(vs[vs!=99.])
Again, this would need to be timed to see how it performed relative to the others.
Problem
I'm trying to speed up the following python code: ``` for j in range(4,len(var_s),3): mag_list.append(float(var_s[j])) mag_list = [value for value in mag_list if value != 99.] med_mag = np.median(mag_list) ``` Is there a nice way to combine the two for-loops into one? This way, it is really slow. What I need is to extract every third entry from the var_s list, beginning with the fifths, if the value of that entry is not equal to 99. Of the resulting list, I need the median. Thanks!