# Minimum and Maximum of a list containing floating point numbers

Discussion in 'Python' started by ceycey, Sep 7, 2010.

1. ### ceyceyGuest

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']. What I want to do is to find minimum and maximum number in
this list.

I used min function,

s = ['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']

print min(s)
print max(s)

these gives me

1.181
9.0601

maximum value is wrong. It must be 10.24.

I know why max function gives wrong number. Because max function
processed elements of list as strings. How can I convert the elements
of list to float so max function finds the correct answer.

I hope I can explain my problem.

Cuneyt.

ceycey, Sep 7, 2010

2. ### MRABGuest

On 07/09/2010 01:44, Xavier Ho wrote:
> On 7 September 2010 10:37, ceycey <
> <mailto:>> 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!

MRAB, Sep 7, 2010

3. ### Tim ChaseGuest

On 09/06/10 19:37, ceycey 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']. What I want to do is to find minimum and maximum number in
> this list.
>
> I used min function,
>
> print min(s)
> print max(s)
>
> these gives me
>
> 1.181
> 9.0601
>
> maximum value is wrong. It must be 10.24.
>
> I know why max function gives wrong number. Because max function
> processed elements of list as strings. How can I convert the elements
> of list to float so max function finds the correct answer.

You can use

min(float(v) for v in s)
max(float(v) for v in s)

to return the floating-point number, or in Python2.5+ you can use

min(s, key=float)
max(s, key=float)

to get the string source. If you need the source string in
pre-2.5, you'd have to do something like

min((float(v), v) for v in s)[1] # 2.4
min([(float(v), v) for v in s])[1] # 2.3 or earlier

and guard against empty input lists.

-tkc

Tim Chase, Sep 7, 2010
4. ### Albert HopkinsGuest

On Mon, 2010-09-06 at 17:37 -0700, ceycey 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']. What I want to do is to find minimum and maximum number in
> this list.
>
> I used min function,
>
> s = ['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']
>
> print min(s)
> print max(s)
>
> these gives me
>
> 1.181
> 9.0601
>
> maximum value is wrong. It must be 10.24.

You are not comparing a list of floats but a list of strings.

> I know why max function gives wrong number. Because max function
> processed elements of list as strings. How can I convert the elements
> of list to float so max function finds the correct answer.

min/max in these cases are returning strings as well. So the fact
remains that the max function is not giving you a number at all, but a
string, and as such is correct. String comparison is not identical to
numerical comparison.

>>> s = ['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']

>>> [type(x) for x in s]

[<type 'str'>, <type 'str'>, <type 'str'>, <type 'str'>, <type 'str'>,
<type 'str'>, <type 'str'>, <type 'str'>, <type 'str'>, <type 'str'>,
<type 'str'>, <type 'str'>, <type 'str'>, <type 'str'>, <type 'str'>,
<type 'str'>, <type 'str'>, <type 'str'>, <type 'str'>, <type 'str'>]

>>> type(max(s))

<type 'str'>

>>> t = [float(x) for x in s]
>>> [type(x) for x in t]

[<type 'float'>, <type 'float'>, <type 'float'>, <type 'float'>, <type
'float'>, <type 'float'>, <type 'float'>, <type 'float'>, <type
'float'>, <type 'float'>, <type 'float'>, <type 'float'>, <type
'float'>, <type 'float'>, <type 'float'>, <type 'float'>, <type
'float'>, <type 'float'>, <type 'float'>, <type 'float'>]
>>> min(t)

1.1880999999999999
>>> max(t)

10.24

>>> type(max(s))

<type 'str'>
>>> type(max(t))

<type 'float'>

Albert Hopkins, Sep 7, 2010
5. ### Steven D'ApranoGuest

On Tue, 07 Sep 2010 11:00:45 +1000, Ben Finney wrote:

> If you're going to use the list of float objects, you can convert them
> all with a list comprehension.

[...]
> >>> numbers_as_float = [float(x) for x in numbers_as_str]

That's awfully verbose. A map is simpler:

numbers_as_float = map(float, numbers_as_str)

--
Steven

Steven D'Aprano, Sep 7, 2010
6. ### nnGuest

On Sep 6, 10:31 pm, Steven D'Aprano <steve-REMOVE-
> wrote:
> On Tue, 07 Sep 2010 11:00:45 +1000, Ben Finney wrote:
> > If you're going to use the list of float objects, you can convert them
> > all with a list comprehension.

> [...]
> >     >>> numbers_as_float = [float(x) for x in numbers_as_str]

>
> That's awfully verbose. A map is simpler:
>
> numbers_as_float = map(float, numbers_as_str)
>
> --
> Steven

In Python 3.x it has one disadvantage:

>>> numbers_as_float = map(float, numbers_as_str)
>>> max(numbers_as_float)

10.24
>>> min(numbers_as_float)

Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
min(numbers_as_float)
ValueError: min() arg is an empty sequence
>>>

nn, Sep 7, 2010