Minimum and Maximum of a list containing floating point numbers

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

  1. ceycey

    ceycey Guest

    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
    #1
    1. Advertising

  2. ceycey

    MRAB Guest

    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
    #2
    1. Advertising

  3. ceycey

    Tim Chase Guest

    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
    #3
  4. 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.

    But to answer your question:

    >>> 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
    #4
  5. 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
    #5
  6. ceycey

    nn Guest

    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
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Motaz Saad
    Replies:
    7
    Views:
    6,482
  2. Ioannis Vranos
    Replies:
    13
    Views:
    489
    James Kanze
    Mar 10, 2008
  3. Replies:
    8
    Views:
    784
    Ben Bacarisse
    Oct 19, 2008
  4. Peng Yu
    Replies:
    16
    Views:
    427
    Philip Potter
    Jan 31, 2010
  5. Marco
    Replies:
    2
    Views:
    104
    Serhiy Storchaka
    Jul 16, 2013
Loading...

Share This Page