Speed ​​up the python cycle

I am 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 good way to combine two for-loops into one? So it is very slow. I need to extract every third record from the var_s list, starting from the fifth, if the value of this record is not 99. From the list I need a median. Thank!

+5
source share
3 answers

You can probably try:

mag_list = [value for value in var_s[4::3] if value != 99.]

depending on var_swhich you can use better itertools.islice(var_s,4,None,3), but it definitely needs to be confined to knowledge.

Perhaps you would do even better if you were stuck with numpy entirely:

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 will need to be timed to see how it is performed relative to others.

+12
source
mag_list = filter(lambda x: x != 99, var_s[4::3])

, timeit, Python 2.7.2:

:

>>> from random import seed, random
>>> from timeit import Timer
>>> from itertools import islice, ifilter, imap
>>> seed(1234); var_s = [random() for _ in range(100)]

for:

>>> def using_for_loop():
...     mag_list = []
...     for j in xrange(4, len(var_s), 3):
...             value = float(var_s[j])
...             if value != 99: mag_list.append(value)
... 
>>> Timer(using_for_loop).timeit()
11.596584796905518

:

>>> def using_map_filter():
...     map(float, filter(lambda x: x != 99, var_s[4::3]))
... 
>>> Timer(using_map_filter).timeit()
8.643505096435547

islice, imap, ifilter:

>>> def using_itertools():
...     list(imap(float, ifilter(lambda x: x != 99, islice(var_s, 4, None, 3)))) 
... 
>>> Timer(using_itertools).timeit()
11.311019897460938

:

>>> def using_list_comp():
...     [float(v) for v in islice(var_s, 4, None, 3) if v != 99]
... 
>>> Timer(using_list_comp).timeit()
8.52650499343872
>>> 

, islice , .

+4
for j in range(4,len(var_s),3):
    value = float(var_s[j])
    if value != 99:
        mag_list.append(value)
med_mag = np.median(mag_list)
+1
source

All Articles