efficient way of computing a list with mean of values in another list

I need to compute a list with the mean values of another list. To be more precise, the input list have this form:

input_list =

['1.538075/42.507325',
 '1.537967/42.507690',
 '1.538292/42.507742',
 '1.538399/42.507376',
 '1.538075/42.507325']

And I need to compute a list with the mean of the values before and after the slash ("/"), like this result:

desired_output =

[1.5381616, 42.5074916]

I can obtain the desired_output correctly using this code:

desired_output = pd.Series(input_list)\
                .apply(lambda r: pd.Series(r.split('/')))\
                .astype(float)\
                .mean()\
                .tolist()

However, I have a very large number of input lists and the proposed code is somewhat slow, so I need to find a more efficient way to do it.

Any suggestions?

>Solution :

Create a numpy array with dtype=float, then calculate mean along axis=0

np.array([s.split('/') for s in input_list], dtype=float).mean(0)

array([ 1.5381616, 42.5074916])

Leave a Reply