Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading