Minimum and Maximum of a list containing floating point numbers

Ben Finney ben+python at benfinney.id.au
Tue Sep 7 03:00:45 CEST 2010


ceycey <cuneyt.ertal at gmail.com> writes:

> 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.

As you correctly describe later, there aren't any numbers in that list;
only strings.

> How can I convert the elements of list to float so max function finds
> the correct answer.

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

    >>> numbers_as_str = ['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']
    >>> numbers_as_float = [float(x) for x in numbers_as_str]
    >>> print min(numbers_as_float), max(numbers_as_float)
    1.1881 10.24

-- 
 \     “As scarce as truth is, the supply has always been in excess of |
  `\                                       the demand.” —Josh Billings |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list