Minimum and Maximum of a list containing floating point numbers

MRAB python at mrabarnett.plus.com
Mon Sep 6 20:56:58 EDT 2010


On 07/09/2010 01:44, Xavier Ho wrote:
> On 7 September 2010 10:37, ceycey <cuneyt.ertal at gmail.com
> <mailto:cuneyt.ertal at gmail.com>> wrote:
>
>     I have a list like ['1.1881', '1.1881', '1.1881', '1.1881', '1.1881',
>     '1.1881', '1.1881', '1.1881', '1.1881', '1.1881', '1.7689', '1.7689',
>     '3.4225', '7.7284', '10.24', '9.0601', '9.0601', '9.0601', '9.0601',
>     '9.0601'].
>
>     How can I convert the elements
>     of list to float so max function finds the correct answer.
>
>
>  >>> input = ['3.4225', '7.7284', '10.24']
>  >>> [float(x) for x in input]
> [3.4225, 7.7284, 10.24]
>
If you wanted to find the maximum value without converting the list to
numbers you could do this:

 >>> input = ['3.4225', '7.7284', '10.24']
 >>> max(input, key=float)
'10.24'

Incidentally, there's a builtin function called 'input' so using it as
a variable name is a discouraged! :-)



More information about the Python-list mailing list