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

Converting a string numbers list into a float numbers list

I`ve been trying to convert a string numbers list into float and get the max of this list but I keep getting this error:

ValueError: could not convert string to float: '.'

My attempt was to get a list like ls = ['1','2','3','0.5'] and convert using this function

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

def convert_to_int(ls: list):
    values = [float(a) for a in ls]
    return (len(values), values)

print(max(ls, key=convert_to_int))

Why I’m having this error? Can someone help me?

>Solution :

I think, what you’re looking for is:

max(ls, key=float)

output: '3'

This will get the max value of your string list, using a temporary float conversion within max

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