Minimum and Maximum of a list containing floating point numbers

nn pruebauno at latinmail.com
Tue Sep 7 10:52:22 EDT 2010


On Sep 6, 10:31 pm, Steven D'Aprano <steve-REMOVE-
T... at cybersource.com.au> 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
>>>



More information about the Python-list mailing list